Python學習筆記:將Dataframe 某一欄中的List轉換成字串(源自stackoverflow) 發佈留言 / Python學習筆記 / 作者: mackuo 問題: 目標: import pandas as pd lists={1:[[1,2,12,6,'ABC']],2:[[1000,4,'z','a']]} #create test dataframe df=pd.DataFrame.from_dict(lists,orient='index') df=df.rename(columns={0:'lists'}) df Out[1]: lists 1 [1, 2, 12, 6, ABC] 2 [1000, 4, z, a] In [2]: # 第一種解法: df['liststring'] = [','.join(map(str, l)) for l in df['lists']] df Out[2]: lists liststring 1 [1, 2, 12, 6, ABC] 1,2,12,6,ABC 2 [1000, 4, z, a] 1000,4,z,a In [3]: # 第二種解法: df['liststring'] = df.lists.apply(lambda x: ', '.join([str(i) for i in x])) df Out[3]: lists liststring 1 [1, 2, 12, 6, ABC] 1, 2, 12, 6, ABC 2 [1000, 4, z, a] 1000, 4, z, a