我正在尝试导入以下csv文本:
name, favorites, age, other_hobbies
joe, "[madonna, elvis, u2]", 28, "[football, cooking]"
mary, "[lady gaga, adele]", 36, "[]"
使用以下pandas
命令
file_name = "new_data.csv"
df = pd.read_csv(file_name, sep =",")
print(df)
我得到了这个结果:
name favorites age other_hobbies
joe "[madonna elvis u2]" 28 "[football cooking]"
mary "[lady gaga adele]" 36 "[]" NaN NaN
为什么会发生这种情况,如何让pandas
正确阅读?
答案 0 :(得分:2)
将skipinitialspace
与sep:
df = pd.read_csv("in.csv",sep="," , skipinitialspace=1)
print(df)
输出:
name favorites age other_hobbies
0 joe [madonna, elvis, u2] 28 [football, cooking]
1 mary [lady gaga, adele] 36 []