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)
最小代码示例
df = pd.DataFrame('x', index=range(3), columns=list('abcde'))df
a b c d e0 x x x x x1 x x x x x2 x x x x x
以下方法都可以工作并产生相同的输出:
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1) # new methoddf2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) # old method
df2
X Y c d e0 x x x x x1 x x x x x2 x x x x x
记住将结果赋值回来,因为修改不是就地的。或者,指定inplace=True:
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)df
X Y c d e0 x x x x x1 x x x x x2 x x x x x
columns = df.columnscolumns = [row.replace("$", "") for row in columns]df.rename(columns=dict(zip(columns, things)), inplace=True)df.head() # To validate the output
new_cols = ['a', 'b', 'c', 'd', 'e']df.columns = new_cols>>> dfa b c d e0 1 1 1 1 1
如果您有一个将旧列名键控到新列名的字典,您可以执行以下操作:
d = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}df.columns = df.columns.map(lambda col: d[col]) # Or `.map(d.get)` as pointed out by @PiRSquared.>>> dfa b c d e0 1 1 1 1 1
如果你没有列表或字典映射,你可以通过列表理解去掉前导$符号:
df.columns = [col[1:] if col[0] == '$' else col for col in df]
df.columns = ['column_one', 'column_two']df.columns.names = ['name of the list of columns']df.index.names = ['name of the index']
name of the list of columns column_one column_twoname of the index0 4 11 5 22 6 3
# new for pandas 0.21+df.some_method1().some_method2().set_axis().some_method3()
# old waydf1 = df.some_method1().some_method2()df1.columns = columnsdf1.some_method3()
def rename(data, oldnames, newname):if type(oldnames) == str: # Input can be a string or list of stringsoldnames = [oldnames] # When renaming multiple columnsnewname = [newname] # Make sure you pass the corresponding list of new namesi = 0for name in oldnames:oldvar = [c for c in data.columns if name in c]if len(oldvar) == 0:raise ValueError("Sorry, couldn't find that column in the dataset")if len(oldvar) > 1: # Doesn't have to be an exact matchprint("Found multiple columns that matched " + str(name) + ": ")for c in oldvar:print(str(oldvar.index(c)) + ": " + str(c))ind = input('Please enter the index of the column you would like to rename: ')oldvar = oldvar[int(ind)]if len(oldvar) == 1:oldvar = oldvar[0]data = data.rename(columns = {oldvar : newname[i]})i += 1return data
下面是它如何工作的一个例子:
In [2]: df = pd.DataFrame(np.random.randint(0, 10, size=(10, 4)), columns = ['col1', 'col2', 'omg', 'idk'])# First list = existing variables# Second list = new names for those variablesIn [3]: df = rename(df, ['col', 'omg'],['first', 'ohmy'])Found multiple columns that matched col:0: col11: col2
Please enter the index of the column you would like to rename: 0
In [4]: df.columnsOut[5]: Index(['first', 'col2', 'ohmy', 'idk'], dtype='object')
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) # Creating a df with column name A and Bdf.rename({"A": "new_a", "B": "new_b"}, axis='columns', inplace =True) # Renaming column A with 'new_a' and B with 'new_b'
Output:
new_a new_b0 1 41 2 52 3 6
使用映射重命名索引/Row_Name:
df.rename({0: "x", 1: "y", 2: "z"}, axis='index', inplace =True) # Row name are getting replaced by 'x', 'y', and 'z'.
Output:
new_a new_bx 1 4y 2 5z 3 6
# This way it will workimport pandas as pd
# Define a dictionaryrankings = {'test': ['a'],'odi': ['E'],'t20': ['P']}
# Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)
# Before renaming the columnsprint(rankings_pd)
rankings_pd.rename(columns = {'test':'TEST'}, inplace = True)