pandas 熊猫数据透视表重命名列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42099024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:55:04  来源:igfitidea点击:

pandas pivot table rename columns

pythonpandaspivotpivot-tabledata-mining

提问by muon

How to rename columns with multiple levels after pandas pivot operation?

如何在Pandas数据透视操作后重命名具有多个级别的列?

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)

gives a test dataframe:

给出一个测试数据框:

   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

applying pivot

应用枢轴

df2 = pd.pivot_table(df, index=["c0"], columns=["c01","c02"], values=["v1","v2"])
df2 = df2.reset_index()

gives

output1

输出1

how to rename the columns by joining levels? with format <c01 value>_<c02 value>_<v1>

如何通过加入级别重命名列?带格式 <c01 value>_<c02 value>_<v1>

for example first column should look like "A_b_v1"

例如第一列应该看起来像 "A_b_v1"

The order of joining levels isn't really important to me.

加入级别的顺序对我来说并不重要。

回答by Psidom

If you want to coalesce the multi-index into a single string index without caring about the index level order, you can simply mapa joinfunction over the columns, and assign the result list back:

如果您想将多索引合并为单个字符串索引而不关心索引级别顺序,您可以简单地对列执行map一个join函数,并将结果列表分配回:

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()

enter image description here

在此处输入图片说明