简体   繁体   English

如何在 pandas pivot 表 function 中重复行标签并将其导出为 ZBF57C906FA7D2BB856D07372E41

[英]how to repeat row labels in pandas pivot table function and export it as excel

I've a sample pivoted data using pandas pivot_table()我有一个使用pandas pivot_table()的示例数据透视表

df = pd.DataFrame([['USA', 'LA', 'April', 2, '1:2'],
                           ['USA', 'FL', 'April', 5, '5:6'],
                           ['USA', 'TX', 'April', 7, '1:3'],
                           ['Canada', 'Ontario', 'April', 2, '1:3'],
                           ['Canada', 'Toronto', 'April', 3, '1:5'],
                           ['USA', 'LA', 'May', 3, '4:5'],
                           ['USA', 'FL', 'May', 6, '4:5'],
                           ['USA', 'TX', 'May', 2, '1:4'],
                           ['Canada', 'Ontario', 'May', 6, '8:9'],
                           ['Canada', 'Toronto', 'May', 9, '3:4']],
             columns=['Country', 'Cities', 'month', 'Count', 'Ratio'])


mux1 = pd.MultiIndex.from_product([data['month'].unique(), ['Count', 'Ratio']])
data = data.pivot_table(columns=['month'], values=['Count', 'Ratio'], index=['Country', 'Cities']).swaplevel(1, 0, axis=1).reindex(mux1, axis=1)


                                April              May
                         Count      Ratio    Count   Ratio
 Country    Cities
   USA       LA           2          1:2      3       4:5
             FL           5          5:6      6       4:5
             TX           7          1:3      2       1:4
   Canada    Ontario      2          1:3      6       8:9
             Toronto      3          1:5      9       3:4

How could I repeat my row labels in the pivot data which looks like below and export it as excel ?如何在如下所示的 pivot 数据中重复我的行标签并将其导出为excel

                                April              May
                         Count      Ratio    Count   Ratio
 Country    Cities
   USA        LA           2         1:2       3      4:5
   USA        FL           5         5:6       6      4:5
   USA        TX           7         1:3       2      1:4
   Canada     Ontario      2         1:3       6      8:9
   Canada     Toronto      3         1:5       9      3:4

I've tried pd.option_context('display.multi_sparse', False) , as it only display the content, it does not export data as excel.我试过pd.option_context('display.multi_sparse', False) ,因为它只显示内容,它不会将数据导出为 excel。

The way I solved it, which may be not the optimal solution was:我解决它的方法可能不是最佳解决方案:

Switching the index order.切换索引顺序。 In your case it would be: index=[ 'Cities','Country'])在您的情况下,它将是: index=[ 'Cities','Country'])

data = data.pivot_table(columns=['month'], values=['Count', 'Ratio'], index=[ 'Cities','Country']).swaplevel(1, 0, axis=1).reindex(mux1, axis=1)

Adding the data.reset_index().to_excel('file.xlsx', index=False) after finishing the table actually worked完成表格后添加 data.reset_index().to_excel('file.xlsx', index=False) 实际工作

cfuper = np.round(pd.pivot_table(pndg_crss, columns = None,
                                        values = 'Results %',
                                        index = ['Email','Title','Manager']))
cfuper.reset_index().to_excel('test.xlsx',sheet_name = 'Sheet1',index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM