2017-09-08 41 views
3

我有一個數據集,看起來像一個數據幀的列:如何挑選到拆散的

UniqueID CategoryType Value 
    A   Cat1  apple 
    A   Cat2  banana 
    B   Cat1  orange 
    C   Cat2  news 
    D   Cat1  orange 
    D   Cat2  blue 

我想它看起來像:

UniqueID Cat1 Cat2 
    A  apple banana 
    B  orange 
    C   news 
    D  orange blue 

我已經嘗試使用取消,但不能得到正確的索引集或東西。

感謝

回答

4

的大部分工作與

df.set_index(['UniqueID', 'CategoryType']).Value.unstack(fill_value='') 

CategoryType Cat1 Cat2 
UniqueID      
A    apple banana 
B    orange   
C      news 
D    orange blue 

完畢後,我們可以得到格式化的休息與

df.set_index(['UniqueID', 'CategoryType']).Value.unstack(fill_value='') \ 
    .rename_axis(None, 1).reset_index() 

    UniqueID Cat1 Cat2 
0  A apple banana 
1  B orange   
2  C   news 
3  D orange blue 
2

可以使用旋轉

編輯:從@ piRsquared的回答中獲得更多的編輯和靈感,

df.pivot('UniqueID', 'CategoryType', 'Value').replace({None: ''}).rename_axis(None, 1).reset_index() 


    UniqueID Cat1 Cat2 
0 A   apple banana 
1 B   orange 
2 C     news 
3 D   orange blue 
+0

外我猜想,這個表現會更好ll數據集〜 – Wen

+0

@Wen,好像是一樣的:) – Vaishali

1

您可以使用pivot_tablefill_value

df.pivot_table(index='UniqueID', columns='CategoryType', values='Value', 
       aggfunc='sum', fill_value='') 

CategoryType Cat1 Cat2 
UniqueID 
A    apple banana 
B    orange 
C      news 
D    orange blue 
0

pivot作品就好了:

df = df.pivot(index = "UniqueID", columns = "CategoryType", values = "Value") 
0

帶我這麼長的時間去思考框:)

index = df.UniqueID.unique() 
columns = df.CategoryType.unique() 
df1= pd.DataFrame(index=index, columns=columns) 
df['match']=df.UniqueID.astype(str)+df.CategoryType 
A=dict(zip(df.match, df.Value)) 
df1.apply(lambda x : (x.index+x.name)).applymap(A.get).replace({None:''}) 

Out[406]: 
    Cat1 Cat2 
A apple banana 
B orange   
C   news 
D orange blue