简体   繁体   中英

Python Pandas adding column values based on condition

I have a DataFrame (df) with following values:

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

I want to add all the countries with values < 25, and show them as 'Others' with their sum (34).

I have created a column name for countries through the following code:

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

Now, I have tried the following code based on another query on StackOverflow:

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

but am getting the following error:

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

Can someone please help? I need the final output as:

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

TIA

Solution with loc for setting with enlargement and filtering by 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM