我有一个数据框,其中包含一个包含列表的列。当我将数据帧写入文件然后重新打开它时,我最终将列表转换为字符串。有没有办法安全地读/写具有列表作为成员的数据帧?
df1 = DataFrame({'a':[['john quincy', 'tom jones', 'jerry rice'],['bob smith','sally ride','little wayne'],['seven','eight','nine'],['ten','eleven','twelve']],'b':[9,2,4,5], 'c': [7,3,0,9]})
df1.to_csv('temp.csv')
df2 = read_csv('temp.csv')
#note how the list (df1) has been converted to a string (df2)
df1['a'][0]
['john quincy', 'tom jones', 'jerry rice']
df2['a'][0]
"['john quincy', 'tom jones', 'jerry rice']"
答案 0 :(得分:2)
首先不需要将列表转换为字符串,列表将自动转换为字符串。只需编写包含列表的数据框,然后在ast.literal_eval
df2
a b c
0 ['john quincy', 'tom jones', 'jerry rice'] 9 7
1 ['bob smith', 'sally ride', 'little wayne'] 2 3
2 ['seven', 'eight', 'nine'] 4 0
3 ['ten', 'eleven', 'twelve'] 5 9
df1.to_csv('temp.csv')
df2 = read_csv('temp.csv')
使用ast.literal_eval
将字符串恢复为列表:
import ast
fd2['a']=df2['a'].apply(lambda x: ast.literal_eval(x))
type(df2['a'][1])
输出:
list
答案 1 :(得分:1)
问题在于:
df2['a'] =df2['a'].map(f)
^^^^^^
f = lambda x : ','.join(x)
再次加入它没有意义,你想把它拆分成一个列表:
df2['a'] = df2['a'].map(lambda L: L.split(','))