简体   繁体   English

df = pd.read_csv('file.csv') 将随机数和逗号添加到文件中。 Pandas 在 python

[英]df = pd.read_csv('file.csv') adds random numbers and commas to file. Pandas in python

I am trying to read a csv file using pandas as so:我正在尝试使用 pandas 读取 csv 文件,如下所示:

df = pd.read_csv('file.csv')

Here is the file before:这是之前的文件:

,schoolId,Name,Meetings Present
0,991,Jimmy Nuetron,2
1,992,Jimmy Fuetron,6
2,993,Cam Nuetron,4

Here is the file after:这是之后的文件:

,Unnamed: 0,schoolId,Name,Meetings Present
0,0.0,991.0,Jimmy Nuetron,2.0
1,1.0,992.0,Jimmy Fuetron,6.0
2,2.0,993.0,Cam Nuetron,4.0
0,,,,3

Why is it adding the numbers and columns when I run the read_csv method?为什么在我运行 read_csv 方法时添加数字和列?

How can I prevent this without adding a seperator?如何在不添加分隔符的情况下防止这种情况发生?

pandas.read_csv is actuallly not adding the column Unnamed: 0 because it already exists in your .csv (who apparently/probably was generated by the method pandas.DataFrame.to_csv ). pandas.read_csv实际上没有添加列Unnamed: 0因为它已经存在于您的.csv中(显然/可能是由方法pandas.DataFrame.to_csv生成的)。

You can get rid of this (extra) column by making it as an index:您可以通过将其作为索引来摆脱这个(额外的)列:

df = pd.read_csv('file.csv', index_col=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM