pandas 数据透视表重命名列 [英] pandas pivot table rename columns
问题描述
如何在熊猫枢轴操作后重命名具有多个级别的列?
How to rename columns with multiple levels after pandas pivot operation?
以下是一些用于生成测试数据的代码:
Here's some code to generate test data:
import pandas as pd
df = pd.DataFrame({
'c0': ['A','A','B','C'],
'c01': ['A','A1','B','C'],
'c02': ['b','b','d','c'],
'v1': [1, 3,4,5],
'v2': [1, 3,4,5]})
print(df)
提供一个测试数据框:
c0 c01 c02 v1 v2
0 A A b 1 1
1 A A1 b 3 3
2 B B d 4 4
3 C C c 5 5
应用数据透视
df2 = pd.pivot_table(df, index=["c0"], columns=["c01","c02"], values=["v1","v2"])
df2 = df2.reset_index()
给予
如何通过加入级别来重命名列?
带有格式
<c01 value>_<c02 value>_<v1>
how to rename the columns by joining levels?
with format
<c01 value>_<c02 value>_<v1>
例如第一列应该看起来像
"A_b_v1"
for example first column should look like
"A_b_v1"
加入关卡的顺序对我来说并不重要.
The order of joining levels isn't really important to me.
推荐答案
如果要在不关心索引级别顺序的情况下将多索引合并为单个字符串索引,则可以简单地map
一个join
函数在各列上,然后将结果列表分配回去:
If you want to coalesce the multi-index into a single string index without caring about the index level order, you can simply map
a join
function over the columns, and assign the result list back:
df2.columns = list(map("_".join, df2.columns))
对于您的问题,您可以遍历每个元素为元组的列,解开元组,然后按所需顺序将它们重新加入:
And for your question, you can loop through the columns where each element is a tuple, unpack the tuple and join them back in the order you want:
df2 = pd.pivot_table(df, index=["c0"], columns=["c01","c02"], values=["v1","v2"])
# Use the list comprehension to make a list of new column names and assign it back
# to the DataFrame columns attribute.
df2.columns = ["_".join((j,k,i)) for i,j,k in df2.columns]
df2.reset_index()
这篇关于 pandas 数据透视表重命名列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!