重命名Pandas中的特定列

重命名Pandas中的特定列

给定一个pandas Dataframe,让我们看看如何使用各种方法来重命名特定的列名。

首先,让我们创建一个Dataframe。

# import pandas package
import pandas as pd
  
# defining a dictionary
d = {"Name": ["John", "Mary", "Helen"],
     "Marks": [95, 75, 99],
     "Roll No": [12, 21, 9]}
  
# creating the pandas data frame
df = pd.DataFrame(d)
  
df
Python

输出:

重命名Pandas中的特定列

方法1:使用Dataframe.rename()

这个方法是在Pandas中重命名所需列的一种方法。它允许我们以字典的形式指定要改变的列名,其键和值为各自列的当前和新名称。

例子1:重命名一个单列。

# import pandas package
import pandas as pd
  
# defining a dictionary
d = {"Name": ["John", "Mary", "Helen"],
     "Marks": [95, 75, 99],
     "Roll No": [12, 21, 9]}
  
# creating the pandas data frame
df = pd.DataFrame(d)
  
# displaying the columns 
# before renaming
print(df.columns)
  
# renaming the column "A"
df.rename(columns = {"Name": "Names"}, 
          inplace = True)
  
# displaying the columns after renaming
print(df.columns)
Python

输出:

重命名Pandas中的特定列

例子2:重命名多列。

# import pandas package
import pandas as pd
  
# defining a dictionary
d = {"Name": ["John", "Mary", "Helen"],
     "Marks": [95, 75, 99],
     "Roll No": [12, 21, 9]}
  
# creating the pandas dataframe
df = pd.DataFrame(d)
  
# displaying the columns before renaming
print(df.columns)
  
# renaming the columns
df.rename({"Name": "Student Name", 
           "Marks": "Marks Obtained", 
           "Roll No": "Roll Number"}, 
          axis = "columns", inplace = True)
  
# displaying the columns after renaming
print(df.columns)
Python

输出:

重命名Pandas中的特定列

示例3:传递lambda函数来重命名列。

# using the same modified dataframe
# df from Renaming Multiple Columns
# this adds ':' at the end 
# of each column name
df = df.rename(columns = lambda x: x+':')
  
# printing the columns
print(df.columns)
Python

输出:

重命名Pandas中的特定列

lambda函数是一个小型匿名函数,可以接受任何数量的参数,但只能有一个表达式。如果我们必须一次性修改所有的列,我们可以使用它。如果列的数量很大,而且使用列表或字典来重命名它们并不是一件容易的事(大量的代码,呼!),那么它就很有用。在上面的例子中,我们用lambda函数在每个列名的末尾加上一个冒号(’:’)。

方法2:使用值属性

我们可以在我们想要重命名的列上使用数值属性,直接改变它。

# using the same modified dataframe 
# df from Renaming Multiple Columns
# Renaming the third column
df.columns.values[2] = "Roll Number" 
  
# printing the columns
print(df.columns)
Python

输出:

重命名Pandas中的特定列

方法3:使用一个**新的列名列表。

我们将更新后的列名以列表的形式传递,以重新命名这些列。我们提供的列表的长度应该与数据框架中的列数相同。否则会发生错误。

# Creating a list of new columns
df_cols = ["Student Name", 
           "Marks Obtained",
           "Roll Number"]
  
# printing the columns 
# before renaming
print(df.columns)
  
# Renaming the columns
df.columns = df_cols
  
# printing the columns 
# after renaming
print(df.columns)
Python

输出:

重命名Pandas中的特定列

方法4:使用Dataframe.columns.str.replace()

一般来说,如果Pandas数据框架中的列的数量很大,比如近100个,而我们想把所有列名中的空格(如果存在的话)替换成下划线。要提供一个列表或字典来重命名所有的列并不容易。因此,我们使用下面的方法—

# printing the column 
# names before renaming
print(df.columns)
  
# Replacing the space in column 
# names by an underscore
df.columns = df.columns.str.replace(' ', '_')
  
# printing the column names 
# after renaming
print(df.columns)
Python

输出:

重命名Pandas中的特定列

另外,其他的字符串方法,如str.lower,可以用来使所有的列名小写。

注意:假设在原始数据框中没有列名,但在为重命名列而提供的字典中存在。默认情况下,rename()函数的错误参数的值为‘忽略’,因此,不会显示错误,并且,现有的列会按照指示重新命名。相反,如果我们将错误参数设置为‘提高’,就会出现一个错误,说明在原始数据框架中不存在特定的列。

下面是一个同样的例子。

例子1:没有提出错误,因为默认情况下,错误被设置为 “忽略”。

# NO ERROR IS RAISED 
  
# import pandas package
import pandas as pd
  
# defining a dictionary
d = {"A": [1, 2, 3], 
     "B": [4, 5, 6]}
  
# creating the pandas dataframe
df = pd.DataFrame(d)
  
# displaying the columns before renaming
print(df.columns)
  
# renaming the columns
# column "C" is not in 
# the original dataframe 
# errors parameter is
# set to 'ignore' by default
df.rename(columns = {"A": "a", "B": "b",
                     "C": "c"}, 
          inplace = True)
  
# displaying the columns
# after renaming
print(df.columns)
Python

输出:

重命名Pandas中的特定列

示例2:将参数errors设置为’raise’。错误被提出(C列在原始数据框架中不存在。)

# ERROR IS RAISED 
  
# import pandas package
import pandas as pd
  
# defining a dictionary
d = {"A": [1, 2, 3],
     "B": [4, 5, 6]}
  
# creating the pandas dataframe
df = pd.DataFrame(d)
  
# displaying the columns
# before renaming
print(df.columns)
  
# renaming the columns
# column "C" is not in the 
# original dataframe setting
# the errors parameter to 'raise'
df.rename(columns = {"A": "a", "B": "b", 
                     "C": "c"}, 
          inplace = True, errors = 'raise')
  
# displaying the columns 
# after renaming
print(df.columns)
Python

输出:

重命名Pandas中的特定列

出现了错误

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册