2017-04-17 45 views
2

列我有一个DF与我想从行转换为列我看到的堆栈溢出大部分的解决方案只有2列Python的大熊猫转换行到多列存在

From DF

PO ID PO Name Region Date Price 
1  AA  North 07/2016 100 
2  BB  South 07/2016 200 
1  AA  North 08/2016 300 
2  BB  South 08/2016 400 
1  AA  North 09/2016 500 
处理多列

To DF

PO ID PO Name Region 07/2016 08/2016 09/2016 
1  AA  North 100  300  500 
2  BB  South 200  400  NaN 
+0

这就是所谓的旋转或拆垛。关于这个 –

+0

哦,我已经搜索了很多关于转换列的问题,大多数问题只处理了2列,因为我有多个列。任何如何在发布前阅读更多内容。 –

回答

3

使用set_indexunstack

df = df.set_index(['PO ID','PO Name','Region', 'Date'])['Price'].unstack() 
print (df) 
Date     07/2016 08/2016 09/2016 
PO ID PO Name Region       
1  AA  North  100.0 300.0 500.0 
2  BB  South  200.0 400.0  NaN 

如果重复需要pivot_tablegroupby聚合函数:

print (df) 
    PO ID PO Name Region  Date Price 
0  1  AA North 07/2016 100 <-for PO ID;PO Name;Region;Date different Price 
1  1  AA North 07/2016 500 <-for PO ID;PO Name;Region;Date different Price 
2  2  BB South 07/2016 200 
3  1  AA North 08/2016 300 
4  2  BB South 08/2016 400 
5  1  AA North 09/2016 500 

df = df.pivot_table(index=['PO ID','PO Name','Region'], 
        columns='Date', 
        values='Price', 
        aggfunc='mean') 
print (df) 
Date     07/2016 08/2016 09/2016 
PO ID PO Name Region       
1  AA  North  300.0 300.0 500.0 <-(100+500)/2=300 for 07/2016 
2  BB  South  200.0 400.0  NaN 

df = df.groupby(['PO ID','PO Name','Region', 'Date'])['Price'].mean().unstack() 
print (df) 
Date     07/2016 08/2016 09/2016 
PO ID PO Name Region       
1  AA  North  300.0 300.0 500.0 <-(100+500)/2=300 for 07/2016 
2  BB  South  200.0 400.0  NaN 

末:

df = df.reset_index().rename_axis(None).rename_axis(None, axis=1) 
print (df) 
    PO ID PO Name Region 07/2016 08/2016 09/2016 
0  1  AA North 300.0 300.0 500.0 
1  2  BB South 200.0 400.0  NaN 
+0

我试过groupby的第三个解决方案,因为set_index对实际数据有重复的影响 –

+0

还有一些问题?如果是的话,你能解释一下吗? – jezrael

+0

第三个解决方案工作得很好。第一个解决方案出错。 ValueError:索引包含重复条目,无法重塑 –