我有一个数据框,我想更改列名。目前我正在使用下面的方法,其中涉及转置、重新索引和转置。必须有一个更简单的方法......
任何建议表示赞赏
import pandas as pd
#make a dataframe with wacky column names
d = {'garbled #### one' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']),
'garbled ### two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
#fix the column names by transposing, reseting index, string manipulation,
#and transposing back
df = df.T
df = df.reset_index()
df['index'] = df['index'].apply(lambda x: x.split()[0]+ " " +x.split()[2])
df = df.set_index('index')
df = df.T
df
index garbled two garbled one
a 1 1
b 2 2
c 3 3
d 4 4
谢谢,扎克cp