pandas 我应该如何使用熊猫读取没有“未命名”行的 csv 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36977223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how should i read a csv file without the 'unnamed' row with pandas?
提问by avicohen
I am writing to a .csv file with:
我正在写入一个 .csv 文件:
my_data_frame.to_cav("some_path")
When trying to read the file with:
尝试使用以下命令读取文件时:
pd.read_csv("some_path")
I can tell that an unnamed column was added. How can i fix that?
我可以看出添加了一个未命名的列。我该如何解决?
回答by MaxU
to_csv()
writes an index per default, so you can either disable index when saving your CSV:
to_csv()
默认情况下写入一个索引,因此您可以在保存 CSV 时禁用索引:
df.to_csv('file.csv', index=False)
or specify an index column when reading:
或者在阅读时指定一个索引列:
df = pd.read_csv('file.csv', index_col=0)