Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a DataFrame using pandas and column labels that I need to edit to replace the original column labels. (我有一个使用熊猫和列标签的DataFrame,我需要对其进行编辑以替换原始列标签。)

I'd like to change the column names in a DataFrame A where the original column names are: (我想在原始列名称为的DataFrame A中更改列名称:)

['$a', '$b', '$c', '$d', '$e'] 

to (至)

['a', 'b', 'c', 'd', 'e'].

I have the edited column names stored it in a list, but I don't know how to replace the column names. (我已经将编辑后的列名存储在列表中,但是我不知道如何替换列名。)

  ask by user1504276 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
234 views
Welcome To Ask or Share your Answers For Others

1 Answer

RENAME SPECIFIC COLUMNS (重命名特定列)

Use the df.rename() function and refer the columns to be renamed. (使用df.rename()函数并引用要重命名的列。) Not all the columns have to be renamed: (并非所有列都必须重命名:)

df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy) 
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)

Minimal Code Example (最小代码示例)

df = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df

   a  b  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

The following methods all work and produce the same output: (下列方法均起作用并产生相同的输出:)

df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1)  # new method
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'})  # old method  

df2

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

Remember to assign the result back, as the modification is not-inplace. (切记将结果分配回去,因为修改未就位。) Alternatively, specify inplace=True : (或者,指定inplace=True :)

df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df

   X  Y  c  d  e
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

From v0.25, you can also specify errors='raise' to raise errors if an invalid column-to-rename is specified. (从v0.25开始,如果指定了无效的要重命名的列,您还可以指定errors='raise'引发错误。) See v0.25 rename() docs . (参见v0.25 named rename() docs 。)


REASSIGN COLUMN HEADERS (REASSIGN列标题)

Use df.set_axis() with axis=1 and inplace=False (to return a copy). (将df.set_axis()axis=1inplace=False (以返回副本)。)

df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1, inplace=False)
df2

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

This returns a copy, but you can modify the DataFrame in-place by setting inplace=True (this is the default behaviour for versions <=0.24 but is likely to change in the future). (这将返回一个副本,但是您可以通过设置inplace inplace=Trueinplace=True修改DataFrame(这是版本<= 0.24的默认行为,但将来可能会更改)。)

You can also assign headers directly: (您还可以直接分配标题:)

df.columns = ['V', 'W', 'X', 'Y', 'Z']
df

   V  W  X  Y  Z
0  x  x  x  x  x
1  x  x  x  x  x
2  x  x  x  x  x

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...