2017-02-24 35 views
5

Giả sử chúng ta có một DataFrame trông như thế này:Làm thế nào để xoay gấu trúc dataframe

day_of_week ice_cream  count proportion 
0 Friday vanilla  638  0.094473 
1 Friday chocolate  2048 0.663506 
2 Friday strawberry 4088 0.251021 
3 Monday vanilla  448  0.079736 
4 Monday chocolate  2332 0.691437 
5 Monday strawberry 441  0.228828 
6 Saturday vanilla  24  0.073350 
7 Saturday chocolate  244  0.712930 ... ... 

Tôi muốn có một DataFrame mới sụp đổ vào day_of_week như một chỉ số để nó trông như thế này:

day_of_week vanilla chocolate strawberry 
0 Friday  0.094473 0.663506 0.251021 
1 Monday  0.079736 0.691437 0.228828 
2 Saturday ...  ...   ... 

Cách sạch nhất tôi có thể thực hiện điều này là gì?

+0

Tra cứu chức năng trục trên gấu trúc – lordingtar

Trả lời

4

df.pivot_table sự là giải pháp đúng:

In[31]: df.pivot_table(values='proportion', index='day_of_week', columns='ice_cream').reset_index() 
Out[31]: 
    ice_cream day_of_week chocolate strawberry vanilla 
0    Friday 0.663506 0.251021 0.094473 
1    Monday 0.691437 0.228828 0.079736 
2   Saturday 0.712930   NaN 0.073350 

Nếu bạn bỏ qua reset_index() nó sẽ thực sự trả về một dataframe lập chỉ mục, trong đó có thể có ích hơn cho bạn.

Lưu ý rằng bảng tổng hợp nhất thiết phải thực hiện giảm kích thước khi cột values không phải là chức năng của bộ tóan (index, columns). Nếu có nhiều cặp (index, columns) khác với valuepivot_table sẽ giảm kích thước xuống một bằng cách sử dụng hàm tổng hợp theo mặc định mean.

+1

'.reset_index() 'để có được kết quả mong muốn của OP? – AChampion

2

Bạn đang tìm kiếm pivot_table

df = pd.pivot_table(df, index='day_of_week', columns='ice_cream', values = 'proportion') 

Bạn nhận:

ice_cream chocolate strawberry vanilla 
day_of_week   
Friday  0.663506 0.251021 0.094473 
Monday  0.691437 0.228828 0.079736 
Saturday 0.712930 NaN   0.073350 
1

Sử dụng trục bảng:

import pandas as pd 
import numpy as np 

df = pd.DataFrame({'day_of_week':['Friday','Sunday','Monday','Sunday','Friday','Friday'], \ 
'count':[200,300,100,50,110,90], 'ice_cream':['choco','vanilla','vanilla','choco','choco','straw'],\ 
'proportion':[.9,.1,.2,.3,.8,.4]}) 

print df 

# If you like replace np.nan with zero 
tab = pd.pivot_table(df,index='day_of_week',columns='ice_cream', values=['proportion'],fill_value=np.nan) 
print tab 

Output:

count day_of_week ice_cream proportion 
0 200  Friday  choco   0.9 
1 300  Sunday vanilla   0.1 
2 100  Monday vanilla   0.2 
3  50  Sunday  choco   0.3 
4 110  Friday  choco   0.8 
5  90  Friday  straw   0.4 
      proportion    
ice_cream  choco straw vanilla 
day_of_week       
Friday   0.85 0.4  NaN 
Monday    NaN NaN  0.2 
Sunday   0.30 NaN  0.1 
+0

Wow bạn thực sự đã dành thời gian để tạo một DataFrame. Bạn biết rằng 'pd.read_clipboard()' tồn tại đúng không? –

1

Sử dụng set_indexunstack

df.set_index(['day_of_week', 'ice_cream']).proportion.unstack() \ 
    .reset_index().rename_axis([None], 1) 

    day_of_week chocolate strawberry vanilla 
0  Friday 0.663506 0.251021 0.094473 
1  Monday 0.691437 0.228828 0.079736 
2 Saturday 0.712930   NaN 0.073350 

thời gian vs pivot_table

enter image description here

Các vấn đề liên quan