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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:09:28  来源:igfitidea点击:

how should i read a csv file without the 'unnamed' row with pandas?

pythoncsvpandas

提问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)