2017-07-19 116 views
0

我試圖改變類似於下面的一個數據幀列:的Python:數據幀轉換爲元組

index apple 
1  [(red,3),(green,2)] 
1  [(red,3)] 
1  [(yellow,9),(red,3)] 
1  [(green,2),(yellow,9)] 
1  [(green,2),(yellow,9), (pink,50)] 
2  [(yellow,14),(red,1)] 
2  [(green,5)] 

到這一點:

index apple_red apple_green apple_yellow apple_pink 
1  3   2   9   50 
2  1   5   14   0 

注意,元組是唯一每個索引中原創表 任何想法如何做到這一點?

感謝

+1

SO沒有代碼編寫的服務。請發佈您迄今爲止嘗試的內容。你遇到什麼問題? –

回答

1

您可以使用:


df1 = pd.DataFrame([dict(x) for x in df['apple']], index=df.index) \ 
     .stack() \ 
     .astype(int) \ 
     .reset_index(name='val') \ 
     .drop_duplicates(['level_1','val']) \ 
     .set_index(['index', 'level_1'])['val'] \ 
     .unstack(fill_value=0) \ 
     .add_prefix('apple_') \ 
     .rename_axis(None) \ 
     .rename_axis(None, axis=1) \ 
     .reset_index() 
print (df1) 
    index apple_green apple_pink apple_red apple_yellow 
0  1   2   50   3    9 
1  2   5   0   1   14 
+0

這太神奇了,非常感謝! – user2475110

+0

很高興可以幫忙,這是有趣的問題! – jezrael