pandas 通过基于另一列的值添加列级别来重塑数据框 [英] Pandas reshape dataframe by adding a column level based on the value of another column
问题描述
我有一个熊猫数据框,我想添加一个列级别,以便根据另一个列的值将特定列(metric_a
,metric_b
,metric_c
)分成几个子列列(parameter
).
I have a pandas dataframe and I would like to add a column level to split specific columns (metric_a
, metric_b
, metric_c
) into several subcolumns based on the value of another column (parameter
).
当前数据格式:
participant param metric_a metric_b metric_c
0 alice a 0,700 0,912 0,341
1 alice b 0,736 0,230 0,370
2 bob a 0,886 0,364 0,995
3 bob b 0,510 0,704 0,990
4 charlie a 0,173 0,462 0,709
5 charlie b 0,085 0,950 0,807
6 david a 0,676 0,653 0,189
7 david b 0,823 0,524 0,430
想要的数据格式:
participant metric_a metric_b metric_c
a b a b a b
0 alice 0,700 0,736 0,912 0,230 0,341 0,370
1 bob 0,886 0,510 0,364 0,704 0,995 0,990
2 charlie 0,173 0,085 0,462 0,950 0,709 0,807
3 david 0,676 0,823 0,653 0,524 0,189 0,430
我尝试过
I have tried
df.set_index(['participant', 'param']).unstack(['param'])
这给了我一个接近的结果,但不满足我的要求,因为我想保留一个单级索引,而participant
保留一个常规列.
which gives me a close result but not satisfies me as I want to keep a single-level index and participant
a regular column.
metric_a metric_b metric_c
param a b a b a b
participant
alice 0,700 0,736 0,912 0,230 0,341 0,370
bob 0,886 0,510 0,364 0,704 0,995 0,990
charlie 0,173 0,085 0,462 0,950 0,709 0,807
david 0,676 0,823 0,653 0,524 0,189 0,430
我凭直觉认为groupby()
或pivot_table()
函数可以完成这项工作,但不知道如何实现.
I have the intuition that groupby()
or pivot_table()
functions could do the job but cannot figure out how.
推荐答案
IIUC,使用 unstack
和 reset_index
指定col_level
参数:
IIUC, use DataFrame.set_index
and unstack
, and reset_index
specifying col_level
parameter:
df.set_index(['participant', 'param']).unstack('param').reset_index(col_level=0)
[出]
participant metric_a metric_b metric_c
param a b a b a b
0 alice 0,700 0,736 0,912 0,230 0,341 0,370
1 bob 0,886 0,510 0,364 0,704 0,995 0,990
2 charlie 0,173 0,085 0,462 0,950 0,709 0,807
3 david 0,676 NaN 0,653 NaN 0,189 NaN
4 heidi NaN 0,823 NaN 0,524 NaN 0,430
这篇关于 pandas 通过基于另一列的值添加列级别来重塑数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!