简体   繁体   English

如何摆脱从 CSV 文件中读取的 pandas DataFrame 中的“未命名:0”列?

[英]How to get rid of "Unnamed: 0" column in a pandas DataFrame read in from CSV file?

I have a situation wherein sometimes when I read a csv from df I get an unwanted index-like column named unnamed:0 .我有一种情况,有时当我从df读取csv时,我会得到一个名为unnamed:0的不需要的类似索引的列。

file.csv

,A,B,C
0,1,2,3
1,4,5,6
2,7,8,9

The CSV is read with this: CSV 是这样读取的:

pd.read_csv('file.csv')

   Unnamed: 0  A  B  C
0           0  1  2  3
1           1  4  5  6
2           2  7  8  9

This is very annoying?这很烦人? Does anyone have an idea on how to get rid of this?有谁知道如何摆脱这个?

It's the index column, pass pd.to_csv(..., index=False) to not write out an unnamed index column in the first place, see the to_csv() docs .它是索引列,传递pd.to_csv(..., index=False)以首先不写出未命名的索引列,请参阅to_csv()文档

Example:例子:

In [37]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
pd.read_csv(io.StringIO(df.to_csv()))

Out[37]:
   Unnamed: 0         a         b         c
0           0  0.109066 -1.112704 -0.545209
1           1  0.447114  1.525341  0.317252
2           2  0.507495  0.137863  0.886283
3           3  1.452867  1.888363  1.168101
4           4  0.901371 -0.704805  0.088335

compare with:与之比较:

In [38]:
pd.read_csv(io.StringIO(df.to_csv(index=False)))

Out[38]:
          a         b         c
0  0.109066 -1.112704 -0.545209
1  0.447114  1.525341  0.317252
2  0.507495  0.137863  0.886283
3  1.452867  1.888363  1.168101
4  0.901371 -0.704805  0.088335

You could also optionally tell read_csv that the first column is the index column by passing index_col=0 :您还可以选择通过传递index_col=0告诉read_csv第一列是索引列:

In [40]:
pd.read_csv(io.StringIO(df.to_csv()), index_col=0)

Out[40]:
          a         b         c
0  0.109066 -1.112704 -0.545209
1  0.447114  1.525341  0.317252
2  0.507495  0.137863  0.886283
3  1.452867  1.888363  1.168101
4  0.901371 -0.704805  0.088335

This is usually caused by your CSV having been saved along with an (unnamed) index ( RangeIndex ).这通常是由于您的 CSV 与(未命名的)索引 ( RangeIndex ) 一起保存造成的。

(The fix would actually need to be done when saving the DataFrame, but this isn't always an option.) (在保存 DataFrame 时实际上需要进行修复,但这并不总是一种选择。)

Workaround: read_csv with index_col=[0] argument解决方法:带有index_col=[0]参数的read_csv

IMO, the simplest solution would be to read the unnamed column as the index . IMO,最简单的解决方案是将未命名的列读取为index Specify an index_col=[0] argument to pd.read_csv , this reads in the first column as the index.pd.read_csv指定一个index_col=[0]参数,这会在第一列中读取为索引。 (Note the square brackets). (注意方括号)。

df = pd.DataFrame('x', index=range(5), columns=list('abc'))
df

   a  b  c
0  x  x  x
1  x  x  x
2  x  x  x
3  x  x  x
4  x  x  x

# Save DataFrame to CSV.
df.to_csv('file.csv')

<!- -> <!- ->

pd.read_csv('file.csv')

   Unnamed: 0  a  b  c
0           0  x  x  x
1           1  x  x  x
2           2  x  x  x
3           3  x  x  x
4           4  x  x  x

# Now try this again, with the extra argument.
pd.read_csv('file.csv', index_col=[0])

   a  b  c
0  x  x  x
1  x  x  x
2  x  x  x
3  x  x  x
4  x  x  x

Note笔记
You could have avoided this in the first place by using index=False if the output CSV was created in pandas, if your DataFrame does not have an index to begin with:如果输出 CSV 是在 pandas 中创建的,如果您的 DataFrame 没有以开头的索引,则您可以首先使用index=False避免这种情况:

 df.to_csv('file.csv', index=False)

But as mentioned above, this isn't always an option.但如上所述,这并不总是一种选择。


Stopgap Solution: Filtering with str.match权宜之计:使用str.match过滤

If you cannot modify the code to read/write the CSV file, you can just remove the column by filtering with str.match :如果您无法修改代码以读取/写入 CSV 文件,则可以通过使用str.match过滤来删除该列

df 

   Unnamed: 0  a  b  c
0           0  x  x  x
1           1  x  x  x
2           2  x  x  x
3           3  x  x  x
4           4  x  x  x

df.columns
# Index(['Unnamed: 0', 'a', 'b', 'c'], dtype='object')

df.columns.str.match('Unnamed')
# array([ True, False, False, False])

df.loc[:, ~df.columns.str.match('Unnamed')]
 
   a  b  c
0  x  x  x
1  x  x  x
2  x  x  x
3  x  x  x
4  x  x  x

要获取所有未命名列,您还可以使用正则表达式,例如df.drop(df.filter(regex="Unname"),axis=1, inplace=True)

Another case that this might be happening is if your data was improperly written to your csv to have each row end with a comma.另一种可能发生这种情况的情况是,如果您的数据被错误地写入csv以使每一行都以逗号结尾。 This will leave you with an unnamed column Unnamed: x at the end of your data when you try to read it into a df .当您尝试将数据读入df时,这将为您留下一个未命名的列Unnamed: x

You can do either of the following with 'Unnamed' Columns:您可以使用“未命名”列执行以下任一操作:

  1. Delete unnamed columns删除未命名的列
  2. Rename them (if you want to use them)重命名它们(如果你想使用它们)

Method 1: Delete Unnamed Columns方法 1:删除未命名的列

# delete one by one like column is 'Unnamed: 0' so use it's name
df.drop('Unnamed: 0', axis=1, inplace=True)

#delete all Unnamed Columns in a single code of line using regex
df.drop(df.filter(regex="Unnamed"),axis=1, inplace=True)

Method 2: Rename Unnamed Columns方法 2:重命名未命名的列

df.rename(columns = {'Unnamed: 0':'Name'}, inplace = True)

If you want to write out with a blank header as in the input file, just choose 'Name' above to be ''.如果您想在输入文件中写出空白标题,只需选择上面的“名称”为“”。

where the OP's input data 'file.csv' was: OP的输入数据'file.csv'是:

,A,B,C
0,1,2,3
1,4,5,6
2,7,8,9

#read file df = pd.read_csv('file.csv') #读取文件df = pd.read_csv('file.csv')

只需使用以下命令删除该列: del df['column_name']

这样做很简单:

df = df.loc[:, ~df.columns.str.contains('^Unnamed')]

或者:

df = df.drop(columns=['Unnamed: 0'])
from IPython.display import display
import pandas as pd
import io


df = pd.read_csv('file.csv',index_col=[0])
df = pd.read_csv(io.StringIO(df.to_csv(index=False)))
display(df.head(5))

A solution that is agnostic to whether the index has been written or not when utilizing df.to_csv() is shown below:使用df.to_csv()时不知道索引是否已写入的解决方案如下所示:

df = pd.read_csv(file_name)
if 'Unnamed: 0' in df.columns:
    df.drop('Unnamed: 0', axis=1, inplace=True)

If an index was not written, then index_col=[0] will utilize the first column as the index which is behavior that one would not want.如果未写入索引,则index_col=[0]将使用第一列作为索引,这是人们不希望的行为。

In my experience, there are many reasons you might not want to set that column as index_col =[0] as so many people suggest above.根据我的经验,您可能不想将该列设置为 index_col =[0] 的原因有很多,因为上面有很多人建议。 For example it might contain jumbled index values because data were saved to csv after being indexed or sorted without df.reset_index(drop=True) leading to instant confusion.例如,它可能包含混乱的索引值,因为数据在没有df.reset_index(drop=True)的情况下被索引或排序后保存到 csv 会导致即时混乱。

So if you know the file has this column and you don't want it, as per the original question, the simplest 1-line solutions are:因此,如果您知道文件有此列并且您不想要它,根据原始问题,最简单的 1 行解决方案是:

df = pd.read_csv('file.csv').drop(columns=['Unnamed: 0'])

or或者

df = pd.read_csv('file.csv',index_col=[0]).reset_index(drop=True)

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

相关问题 如何摆脱 Pandas csv 文件中的“未命名 0”? - How can I get rid of 'Unnamed 0' in Pandas csv file? 熊猫read_csv不会将最终(未命名)列捕获到数据框中 - pandas read_csv does not capture final (unnamed) column into dataframe 如何使用 DASK dataframe 读取 csv 以使其没有“未命名:0”列? - How to read in csv with to to a DASK dataframe so it will not have “Unnamed: 0” column? 为什么从 pandas 读取的 csv 文件会得到未命名的列? - Why does my csv file read from pandas get unnamed columns? 从 csv 文件 Pandas Python 中删除未命名的列 - Deleting an unnamed column from a csv file Pandas Python Pandas:如何从没有索引的 csv 文件中读取多索引列 dataframe? - Pandas: how to read a multi-index column dataframe from a csv file without an index? 如何在streamlit中从用户读取csv文件并转换为pandas数据帧 - How to read csv file from user in streamlit and converting to pandas dataframe "重命名未命名的列熊猫数据框" - Rename unnamed column pandas dataframe 如何摆脱 Pandas dataframe 中的索引列名称? - How to get rid of the index column name in a Pandas dataframe? 为什么熊猫数据框将列名显示为&#39;未命名:1&#39;,未命名:2&#39;,.......&#39;未命名:n&#39; - Why pandas dataframe displaying column names as 'unnamed: 1', unnamed: 2',.......'unnamed: n'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM