2016-09-16 31 views
0

我有一個數據幀是一般是這樣的:如何組合多個行到一個基於共享價值熊貓

df = pd.DataFrame({'Country': ['USA', 'USA', 'Canada', 'Canada'], 'GDP':   [45000, 68000, 34000, 46000], 'Education': [5, 3, 7, 9]}) 

,並提供:

Country Education GDP 
0  USA   5 45000 
1  USA   3 68000 
2 Canada   7 34000 
3 Canada   9 46000 

我想知道所有的在同一行上列出的每個國家/地區的值如下:

Country Education Education GDP  GDP 
USA   5   3   45000  68000 

如何完成此操作?

是的,一些列確實有相同的名稱。

謝謝。

回答

1

原始數據幀:

In [150]: df 
Out[150]: 
    Country Education GDP 
0  USA   5 45000 
1  USA   3 68000 
2 Canada   7 34000 
3 Canada   9 46000 

鑑於each country將會有確切的兩個值屬性相同:

In [151]: df1 = df.groupby('Country').nth(0).reset_index() 

In [152]: df1 
Out[152]: 
    Country Education GDP 
0 Canada   7 34000 
1  USA   5 45000 

In [153]: df2 = df.groupby('Country').nth(1).reset_index() 

In [154]: df2 
Out[154]: 
    Country Education GDP 
0 Canada   9 46000 
1  USA   3 68000 

Concat兩個數據幀和drop重複列從任何一個:

In [155]: pd.concat([df1, df2.drop('Country', 1)], axis=1) 
Out[155]: 
    Country Education GDP Education GDP 
0 Canada   7 34000   9 46000 
1  USA   5 45000   3 68000 

重新排列列,如果需要:

In [165]: df3 = pd.concat([df1, df2.drop('Country', 1)], axis=1) 

In [166]: df3 = df3[['Country', 'Education', 'GDP']] 

In [167]: df3 
Out[167]: 
    Country Education Education GDP GDP 
0 Canada   7   9 34000 46000 
1  USA   5   3 45000 68000 
+0

這是非常有用的,我將其標記爲回答。但是我確實有一個問題:使用reset_index()方法的目的是什麼? –

+0

@JonathanBechtel http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html –

1

您想要的輸出通常會導致信息丟失。

Country Education Education GDP  GDP 
USA   5   3   45000  68000 

在上述情況下,您需要跟蹤哪個GDP列對應於哪個教育列。

如果你不堅定的關於保持它在這種形式下,可以形成一個透視表:

df2=df.pivot(index='Country',columns='Education',values='GDP').reset_index() 

這使得教育作爲一列,該列的值的每個唯一值將是相應的GDP值。

Education Country  3  5  7  9 
0   Canada  NaN  NaN 34000.0 46000.0 
1    USA 68000.0 45000.0  NaN  NaN 

更好看輸出可通過以下步驟獲得:

df2=df.pivot(index='Country',columns='Education',values='GDP').reset_index().set_index('Country') 

其產生

Country  3   5   7   9 
Canada       34000.0  46000.0 
USA   68000.0  45000.0  
相關問題