2016-07-25 283 views
2

我有DF:熊貓:調換數據幀

ID url visits count_sec buys 
0012ea90a6deb4eeb2924fb13e844136 aliexpress.com 3438 33067 25 
0012ea90a6deb4eeb2924fb13e844136 ebay.com 9 44 
001342afb153e2775649dc5ae0460605 ozon.ru 1 6 
0019b08bc9bb8da21f3b8ecc945a67d3 aliexpress.com 24 2196  
0019b08bc9bb8da21f3b8ecc945a67d3 bonprix.ru 42 1378  

我需要transponse,並得到

ID           url 
            visits count_sec buys 
            aliexpress.com   ebay.com   ozon.ru  aliexpress.com  bonprix.ru 
0012ea90a6deb4eeb2924fb13e844136 3438 33067 25  9 44 0  0 0 0  0 0 0   0 0 0 
001342afb153e2775649dc5ae0460605 0 0 0   0 0 0   1 6 0  0 0 0   0 0 0 
0019b08bc9bb8da21f3b8ecc945a67d3 0 0 0   0 0 0   0 0 0  24 2196 0   42 1378 0 

我怎麼能這樣做?

+0

你需要在Multiindex列中的頂級'url'嗎? – jezrael

回答

3

您可以使用pivot_table,但在MultiIndex列中缺少頂部url。也值只是隨機的,因爲它不清楚每列包含值:

#new column with value `url` 
df['url1'] = 'url' 
df1 = df.pivot_table(index='ID', values=['visits','count_sec','buys'],columns=['url1','url']) 
#swap first and second level in MultiIndex in columns 
df1.columns = df1.columns.swaplevel(0,1) 
#remove columns names 
df1 = df1.rename_axis((None,None,None), axis=1) 
print (df1) 
              url        \ 
             visits        
           aliexpress.com bonprix.ru ebay.com ozon.ru 
ID                    
0012ea90a6deb4eeb2924fb13e844136   3438.0  NaN  NaN  NaN 
001342afb153e2775649dc5ae0460605   NaN  NaN  NaN  NaN 
0019b08bc9bb8da21f3b8ecc945a67d3   NaN  NaN  NaN  NaN 

                      \ 
             count_sec        
           aliexpress.com bonprix.ru ebay.com ozon.ru 
ID                    
0012ea90a6deb4eeb2924fb13e844136  33067.0  NaN  9.0  NaN 
001342afb153e2775649dc5ae0460605   NaN  NaN  NaN  1.0 
0019b08bc9bb8da21f3b8ecc945a67d3   24.0  42.0  NaN  NaN 


              buys        
           aliexpress.com bonprix.ru ebay.com ozon.ru 
ID                   
0012ea90a6deb4eeb2924fb13e844136   25.0  NaN  44.0  NaN 
001342afb153e2775649dc5ae0460605   NaN  NaN  NaN  6.0 
0019b08bc9bb8da21f3b8ecc945a67d3   2196.0  1378.0  NaN  NaN 
+0

在頂部執行'url'並且在'visit,count_sec,buys'下面是真的嗎? – ldevyataykina

+0

我問了關於'merge'df的問題。你問我'result = pd.merge(short,short1,on ='ID',how ='outer') '。但我怎樣才能合併'數據透視表'? – ldevyataykina

+0

請參閱編輯,我添加頂級'url'。 – jezrael