pd.read_csv添加名为“未命名:0"的列 [英] pd.read_csv add column named "Unnamed: 0
问题描述
我有一个包含3列的数据框.我用 pd.to_csv(filename)
保存然后使用
I have a dataframe with 3 columns.
I save with pd.to_csv(filename)
and then re-open it with
pd.read_csv(filename, index_col=False)
但是我得到一个包含4列的数据框,最左边的列叫做
But I get a dataframe with 4 columns, with the left-most column called
未命名:0
实际上只是行号.没有它,我怎么能读取csv?
that is actually just the row number. How can I read the csv without it?
谢谢!
推荐答案
您应该尝试:
pd.read_csv('file.csv', index_col=0)
index_col:int或sequence或False,默认为None列用作DataFrame的行标签.如果给出了序列,则为MultiIndex用来.如果您的格式错误的文件末尾带有定界符每行,您可能会认为index_col = False强制熊猫不要使用第一列作为索引(行名)
index_col : int or sequence or False, default None Column to use as the row labels of the DataFrame. If a sequence is given, a MultiIndex is used. 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)
示例数据集:
我从Google那里获取了数据集,因此,当我只是尝试使用pd.read_csv导入数据时,它默认显示 Unnamed:0
.
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.
>>> 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
因此,为了避免 Unnamed:0
,我们必须使用 index_col = 0
并获得更好的数据框:
So, Just to avoid the the Unnamed: 0
we have to use index_col=0
and will get the nicer dataframe:
>>> 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
注意:因此,为了更清楚地理解我们说的是 index_col = 0
时,它将第一列作为dataFrame中的索引放置,而不显示为未命名:0
.
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
.
希望这会有所帮助.
这篇关于pd.read_csv添加名为“未命名:0"的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!