2017-02-06 102 views
3

我有一個數據幀(DF)與以下值:Python的熊貓加列值基於條件

    Title 
fintech_countries   
US     60 
UK     54 
India    28 
Australia   25 
Germany   13 
Singapore   11 
Canada    10 

我想所有國家增加其值< 25,並顯示他們與「其他」的總和(34)。

我已經通過下面的代碼創建一個列名的國家:

df1 = df.rename_axis('fintech_countries').rename_axis("countries", axis="columns" , inplace=True) 


countries   Title 
fintech_countries   
US     60 
UK     54 
India    28 
Australia   25 
Germany   13 
Singapore   11 
Canada    10 

現在,我已經嘗試基於另一個查詢StackOverflow上下面的代碼:

df1.loc[df1['Title'] < 25, "countries"].sum() 

,但我得到的以下錯誤:

KeyError: 'the label [countries] is not in the [columns]' 

有人可以幫忙嗎?我需要的最終輸出爲:

countries   Title 
fintech_countries   
US     60 
UK     54 
India    28 
Australia   25 
Others    34 

TIA

回答

3

解決方案與locsetting with enlargement和過濾通過boolean indexing

mask = df['Title'] < 25 
print (mask) 
fintech_countries 
US   False 
UK   False 
India  False 
Australia False 
Germany  True 
Singapore  True 
Canada  True 
Name: Title, dtype: bool 

df1 = df[~mask].copy() 
df1.loc['Others', 'Title'] = df.loc[mask, 'Title'].sum() 
df1.Title = df1.Title.astype(int) 
print (df1) 
countries   Title 
fintech_countries  
US     60 
UK     54 
India     28 
Australia    25 
Others    34 
+0

由於一噸!有效 – chhibbz