2017-08-10 87 views
2

我有以下代碼:熊貓GROUPBY存儲在一個新的數據幀

import pandas as pd 
df1 = pd.DataFrame({'Counterparty':['Bank','Bank','GSE','PSE'], 
      'Sub Cat':['Tier1','Small','Small', 'Small'], 
      'Location':['US','US','UK','UK'], 
      'Amount':[50, 55, 65, 55], 
      'Amount1':[1,2,3,4]}) 

df2=df1.groupby(['Counterparty','Location'])[['Amount']].sum() 
df2.dtypes 
df1.dtypes 

的DF2數據幀沒有,我彙總了各列(交易對手和地點)。任何想法,爲什麼是這種情況?金額和金額1都是數字字段。我只是想從指數跨越Amount1

+0

http://pandas.pydata.org/pandas-docs/stable/groupby.html#automatic-exclusion-of-nuisance-columns –

回答

7

跨越金額和總總結爲列添加as_index=False參數或reset_index:由所有列

df2=df1.groupby(['Counterparty','Location'])[['Amount']].sum().reset_index() 
print (df2) 
    Counterparty Location Amount 
0   Bank  US  105 
1   GSE  UK  65 
2   PSE  UK  55 

df2=df1.groupby(['Counterparty','Location'], as_index=False)[['Amount']].sum() 
print (df2) 
    Counterparty Location Amount 
0   Bank  US  105 
1   GSE  UK  65 
2   PSE  UK  55 

如果聚集在這裏發生automatic exclusion of nuisance columns - 省略列Sub Cat

df2=df1.groupby(['Counterparty','Location']).sum().reset_index() 
print (df2) 
    Counterparty Location Amount Amount1 
0   Bank  US  105  3 
1   GSE  UK  65  3 
2   PSE  UK  55  4 


df2=df1.groupby(['Counterparty','Location'], as_index=False).sum() 
+1

添加'.reset_index()'總是很不錯的。 –

+0

與.reset_index()一起工作。將不得不閱讀爲什麼.reset_index()是必要的! –

+0

超級,抱歉,首先我不明白你的問題。如果我的回答有幫助,請不要忘記[接受](http://meta.stackexchange.com/a/5235/295067) - 點擊答案旁邊的複選標記('✓')將其從灰色出來填補。謝謝。 – jezrael

0

刪除圍繞'Amount'的雙括號,並使它們成爲單個括號。你告訴它只選擇一列。