简体   繁体   English

pd.read_csv添加名为“未命名的列:0

[英]pd.read_csv add column named "Unnamed: 0

I have a dataframe with 3 columns. 我有一个3列的数据框。 I save with pd.to_csv(filename) and then re-open it with 我用pd.to_csv(filename)保存,然后用重新打开

pd.read_csv(filename, index_col=False)

But I get a dataframe with 4 columns, with the left-most column called 但是我得到了一个包含4列的数据框,最左边的列称为

Unnamed:0 无名:0

that is actually just the row number. 那实际上只是行号。 How can I read the csv without it? 没有它,我如何阅读csv?

Thanks! 谢谢!

You should try: 你应该试试:

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

index_col : int or sequence or False, default None Column to use as the row labels of the DataFrame. index_col:int或sequence或False,默认为None用作DataFrame的行标签的列。 If a sequence is given, a MultiIndex is used. 如果给出了序列,则使用MultiIndex。 If you have a malformed file with delimiters at the end of each line, you might consider index_col=False to force pandas to not use the first column as the index (row names) 如果您在每行末尾都有一个带有定界符的格式错误的文件,则可以考虑index_col = False来强制熊猫不要将第一列用作索引(行名)

Example Dataset: 示例数据集:

I have taken the dataset from google,So while i'm simply trying to import the data with pd.read_csv it shows the Unnamed: 0 as default. 我已经从Google那里获取了数据集,因此,当我只是尝试使用pd.read_csv导入数据时,它默认显示Unnamed: 0

>>> df = pd.read_csv("amis.csv")
>>> df.head()
   Unnamed: 0  speed  period  warning  pair
0           1     26       1        1     1
1           2     26       1        1     1
2           3     26       1        1     1
3           4     26       1        1     1
4           5     27       1        1     1

So, Just to avoid the the Unnamed: 0 we have to use index_col=0 and will get the nicer dataframe: 因此,为了避免Unnamed: 0我们必须使用index_col=0并获得更好的数据帧:

>>> df = pd.read_csv("amis.csv", index_col=0)
>>> df.head()
   speed  period  warning  pair
1     26       1        1     1
2     26       1        1     1
3     26       1        1     1
4     26       1        1     1
5     27       1        1     1

Note : So, to make it more explicit to understand when we say index_col=0 , it placed the first column as the index in the dataFrame rather appearing as Unnamed: 0 . 注意 :因此,为了更清楚地理解我们说的index_col=0 ,它将第一列作为dataFrame中的索引放置,而不是显示为Unnamed: 0

Hope this will help. 希望这会有所帮助。

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

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