简体   繁体   English

"重命名未命名的列熊猫数据框"

[英]Rename unnamed column pandas dataframe

My csv file has no column name for the first column, and I want to rename it.我的 csv 文件没有第一列的列名,我想重命名它。 Usually, I would do data.rename(columns={'oldname':'newname'}, inplace=True)<\/code> , but there is no name in the csv file, just ''.通常,我会做data.rename(columns={'oldname':'newname'}, inplace=True)<\/code> ,但 csv 文件中没有名称,只有 ''。

"

You can view the current dataframe using data.head()您可以使用data.head()查看当前数据帧

if that returns 'Unnamed: 0' as the column title, you can rename it in the following way:如果返回'Unnamed: 0'作为列标题,您可以按以下方式重命名:

data.rename( columns={'Unnamed: 0':'new column name'}, inplace=True )

When you load the csv, use the option 'index_col' like加载 csv 时,请使用“index_col”选项,例如

pd.read_csv('test.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 强制熊猫使用第一列作为索引(行名称)

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.parsers.read_csv.html http://pandas.pydata.org/pandas-docs/dev/generated/pandas.io.parsers.read_csv.html

The solution can be improved as data.rename( columns={0 :'new column name'}, inplace=True ) .该解决方案可以改进为data.rename( columns={0 :'new column name'}, inplace=True ) There is no need to use 'Unnamed: 0' , simply use the column number, which is 0 in this case and then supply the 'new column name' .无需使用'Unnamed: 0' ,只需使用列号,在本例中为0 ,然后提供'new column name'

这应该有效:

data.rename( columns={0 :'Articles'}, inplace=True )

试试下面的代码,

df.columns = ['A', 'B', 'C', 'D']

usually the blank column names are named based on their index通常空白列名是根据它们的索引命名的

so for example lets say the 4 column is unnamed.例如,假设第 4 列未命名。

df.rename({'unnamed:3':'new_name'},inplace=True)

usually it is named like this since the indexing of columns start with zero.通常它是这样命名的,因为列的索引从零开始。

It has a name, the name is just '' (the empty string).它有一个名字,名字只是'' (空字符串)。

In [2]: df = pd.DataFrame({'': [1, 2]})

In [3]: df
Out[3]: 

0  1
1  2

In [4]: df.rename(columns={'': 'A'})
Out[4]: 
   A
0  1
1  2

It can be that the first column/row could not have a name, because it's an index and not a column/row.可能是第一列/行没有名称,因为它是索引而不是列/行。 That's why you need to rename the index like this:这就是为什么你需要像这样重命名索引:

df.index.name = 'new_name'

Q: "my column has no name when I print the DataFrame it shows the column 0 only and I used this technique its not helping me, can you please suggest any other method to replace the name"问:“当我打印 DataFrame 时,我的列没有名称,它只显示列 0,我使用了这种技术,它对我没有帮助,您能否建议任何其他方法来替换名称”

A: A:

data.rename( columns={0:'new column name'}, inplace=True )

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

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