pandas 创建具有多列的透视表

tvz2xvvm  于 2023-03-06  发布在  其他
关注(0)|答案(1)|浏览(158)

我正在尝试创建一个具有多列的数据透视表;我不知道如何更好地解释这个问题,但以下是所需的输出, Dataframe 设置,和代码,我已经尝试了。

**所需输出:x1c 0d1x
** Dataframe 设置:

data = {
 'WholesalerID': {0: 121, 1: 121, 2: 42, 3: 42, 4: 54, 5: 43, 6: 432, 7: 4245, 8: 4245, 9: 4245, 10: 457},
 'Brand': {0: 'Vans', 1: 'Nike', 2: 'Nike', 3: 'Vans',4: 'Vans', 5: 'Nike', 6: 'Puma', 7: 'Vans', 8: 'Nike', 9: 'Puma', 10: 'Converse'},
 'Shop 1': {0: 'Yes', 1: 'No', 2: 'Yes', 3: 'Maybe', 4: 'Yes', 5: 'No', 6: 'Yes', 7: 'Yes', 8: 'Maybe', 9: 'Maybe', 10: 'No'},
 'Shop 2': {0: 'No', 1: 'Yes', 2: 'Maybe', 3: 'Maybe', 4: 'Yes', 5: 'No', 6: 'No', 7: 'No', 8: 'Maybe', 9: 'Yes', 10: 'Yes'}
}

df = pd.DataFrame.from_dict(data)

旋转尝试:

df = df.assign(count = 1)
pivoted_df = pd.pivot_table(df,
                            index = ['Brand'],
                            columns = ['Shop 1', 'Shop 2'],
                            values = ['count'],
                            aggfunc = {'count': 'count'},
                            fill_value = 0,
                            margins = True,
                            margins_name = 'Total'
)
hivapdat

hivapdat1#

使用DataFrame.melt进行逆透视,然后将列value转换为Categorical以正确排序最终MultiIndex,按crosstab整形,按列的第一级添加小计,并使用添加级Total追加到原始列:

df = df.melt(id_vars='Brand', value_vars=['Shop 1', 'Shop 2'])
df['value'] = pd.Categorical(df['value'], ordered=True, categories=df['value'].unique())
                 .add_categories(['Total'])

pivoted_df = pd.crosstab(df['Brand'], [df['variable'], df['value']])

totals = (pd.concat({'Total': pivoted_df.groupby(level=0, axis=1).sum()}, axis=1)
            .swaplevel(axis=1))

pivoted_df = pd.concat([pivoted_df, totals],axis=1).sort_index(axis=1)
print (pivoted_df)
variable Shop 1                Shop 2               
value       Yes No Maybe Total    Yes No Maybe Total
Brand                                               
Converse      0  1     0     1      1  0     0     1
Nike          1  2     1     4      1  1     2     4
Puma          1  0     1     2      1  1     0     2
Vans          3  0     1     4      1  2     1     4

相关问题