我有一个看起来像的熊猫数据框。
week_of total_atc rx_atc non_rx_atc custom_atc
0 15 43889 28950.0 14939.0 4979.0
1 16 114112 75477.0 38635.0 12471.0
2 17 127423 84852.0 42571.0 13945.0
所需的输出
15 16 17
total_atc 43889 114112 127423
rx_atc 28950.0 75477.0 84852.0
non_rx_atc 14939 38635 42571
尝试了类似
products_cart.pivot(index = 'week_of',columns='week_of',values='total_atc')
答案 0 :(得分:1)
您可以使用set_index
+ transpose
(或其别名T
):
res = df.set_index('week_of').T
print(res)
week_of 15 16 17
total_atc 43889.0 114112.0 127423.0
rx_atc 28950.0 75477.0 84852.0
non_rx_atc 14939.0 38635.0 42571.0
custom_atc 4979.0 12471.0 13945.0
如果您需要删除custom_atc
索引,则可以使用pd.DataFrame.drop
:
res = res.drop('custom_atc', 0)
print(res)
week_of 15 16 17
total_atc 43889.0 114112.0 127423.0
rx_atc 28950.0 75477.0 84852.0
non_rx_atc 14939.0 38635.0 42571.0