如何根据结果旋转 Dataframe 组

how to pivot a Dataframe groupby results

我有以下数据框:

test=pd.DataFrame({'MKV':[50,1000,80,20],
                  'Rating':['A','Z','A','A'],
                  'Sec':['I','I','I','F']})

test.groupby(['Rating','Sec'])['MKV'].apply(lambda x: x/x.sum())
gives results:
0   0.38
1   1.00
2   0.62
3   1.00

如何根据结果对这个分组进行透视,以将每个组的结果放入单独的列中?

我认为您不需要 groupby。您可以使用 set_index and unstack 进行数据透视,然后对列进行归一化:

# Perform the pivot.
test = test.set_index(['Rating','Sec'], append=True).unstack(['Rating','Sec'])

# Normalize the columns.
test = test/test.sum()

# Rename columns as appropriate.
test.columns = [','.join(c[1:]) for c in test.columns]

结果输出:

        A,I  Z,I  A,F
0  0.384615  NaN  NaN
1       NaN  1.0  NaN
2  0.615385  NaN  NaN
3       NaN  NaN  1.0