2017-02-06 1 views
3

에 따라 :파이썬 팬더 다음과 같은 값으로 나는 DataFrame (DF)가 조건

    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 

, 나는에 유래에 다른 쿼리를 기반으로 다음과 같은 코드를 시도 :

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

을하지만 무엇입니까 다음 오류 :

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

누군가 도움을 주시겠습니까? boolean indexing에 의해

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

TIA setting with enlargement 및 필터링 loc

답변

3

솔루션 : 나는대로 최종 출력을 필요로

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

관련 문제