我有一个名为data'的数据框架。我如何重命名只有一列的标题?例如将
gdp改为
log(gdp)`?
data =
y gdp cap
0 1 2 5
1 2 3 9
2 8 7 2
3 3 4 7
4 6 7 7
5 4 8 3
6 8 2 8
7 9 9 10
8 6 6 4
9 10 10 7
如果你需要重命名一个单一的列,一个更快的实现是使用list-comprehension
。
df.columns = ['log(gdp)' if x=='gdp' else x for x in df.columns]
如果需要对多列进行重命名,可以使用条件表达式,比如:
df.columns = ['log(gdp)' if x=='gdp' else 'cap_mod' if x=='cap' else x for x in df.columns]
或者,使用 "字典 "构建一个映射,通过设置默认值作为旧名称,用它的 "获取 "操作执行 "列表理解":
col_dict = {'gdp': 'log(gdp)', 'cap': 'cap_mod'} ## key→old name, value→new name
df.columns = [col_dict.get(x, x) for x in df.columns]
时间:
%%timeit
df.rename(columns={'gdp':'log(gdp)'}, inplace=True)
10000 loops, best of 3: 168 µs per loop
%%timeit
df.columns = ['log(gdp)' if x=='gdp' else x for x in df.columns]
10000 loops, best of 3: 58.5 µs per loop
我如何在pandas中重命名一个特定的列?
从v0.24+开始,要一次重命名一个(或多个)列、
DataFrame.rename()
用axis=1
或axis='columns'
(axis
参数是在v0.21
引入的。
Index.str.replace()
用于基于字符串/regex的替换。
如果你需要一次性重命名所有列、
DataFrame.set_axis()
方法使用axis=1
。传递一个类似列表的序列。选项也可用于就地修改。重命名'与
轴=1'。df = pd.DataFrame('x', columns=['y', 'gdp', 'cap'], index=range(5))
df
y gdp cap
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
在0.21以上版本中,你现在可以用rename'指定一个
轴'参数:
df.rename({'gdp':'log(gdp)'}, axis=1)
# df.rename({'gdp':'log(gdp)'}, axis='columns')
y log(gdp) cap
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
(注意,rename
默认不是就地取材,所以你需要将结果分配回来)。
增加这个参数是为了提高与其他API的一致性。新的轴'参数类似于
列'参数--它们做同样的事情。
df.rename(columns={'gdp': 'log(gdp)'})
y log(gdp) cap
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
rename
也接受一个回调,对每一列调用一次。
df.rename(lambda x: x[0], axis=1)
# df.rename(lambda x: x[0], axis='columns')
y g c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
在这种特定情况下,你会希望使用
df.rename(lambda x: 'log(gdp)' if x == 'gdp' else x, axis=1)
Index.str.replace
类似于python中字符串的replace
方法,pandas索引和系列(仅对象dtype)定义了一个("矢量化")str.replace
方法用于字符串和基于regex的替换。
df.columns = df.columns.str.replace('gdp', 'log(gdp)')
df
y log(gdp) cap
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
与其他方法相比,该方法的优势在于str.replace
支持重词(默认启用)。更多信息请参见文档。
set_axis'传递一个列表,并使用
axis=1'。用一个头的列表调用set_axis'.该列表的长度必须与列/索引的大小相等。
set_axis默认情况下是对原始DataFrame进行修改,但是你可以指定
inplace=False`来返回一个修改过的副本。
df.set_axis(['cap', 'log(gdp)', 'y'], axis=1, inplace=False)
# df.set_axis(['cap', 'log(gdp)', 'y'], axis='columns', inplace=False)
cap log(gdp) y
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
注意:在未来的版本中,inplace
将默认为True
。
方法链
既然我们已经有了df.columns = ...'的有效方法来分配列,为什么还要选择
set_axis'呢?正如Ted Petrou在[本答案]中所展示的,(https://stackoverflow.com/a/46912050/4909087) set_axis
在尝试连锁方法时很有用。
比较
# new for pandas 0.21+
df.some_method1()
.some_method2()
.set_axis()
.some_method3()
对比
# old way
df1 = df.some_method1()
.some_method2()
df1.columns = columns
df1.some_method3()
前者是更自然、更自由的句法。