2

given this dataframe is it possible to look for particular strings such as the countries that are located inside the countries list? (For example for the first index in 'Country', it has the word Japan inside it and its corresponding value will be 1). Is it possible to sum up the value that corresponds to each country? (End result: Japan: 1+3=4 USA:2 Europe:4)

countries=["Europe","USA","Japan"]
df=pd.DataFrame={'Employees':[1,2,3,4],
                 'Country':['Japan;Security','USA;Google',"Japan;Sega","Europe;Google"]}
print(df)

Thanks

2
  • I'm on mobile, but here it goes. I would make a new column with .str.split(';')[0] then do a groupby on the new column, use .agg({'Employees':'sum'}). This is a classic use case of groupby, I strongly encourage you to read the docs. Commented May 1, 2021 at 13:32
  • alright thanks for the advice Commented May 1, 2021 at 13:35

2 Answers 2

2

If you wanna use only those values specified in the country list. You can do something like this -

patt = '(' + '|'.join(countries) + ')'
grp = df.Country.str.extract(pat=patt, expand=False).values
new_df = df.groupby(grp).agg({'Employees': sum})

For example, if the initial country list is missing 'JAPAN' -

countries = ["Europe", "USA"]
patt = '(' + '|'.join(countries) + ')'
grp = df.Country.str.extract(pat=patt, expand=False).values
new_df = df.groupby(grp, dropna=False).agg({'Employees': sum}).reset_index().rename(
    columns={'index': 'Country'}).fillna('other')

outptut-

  Country  Employees
0  Europe          4
1     USA          2
2   other          4 # see the change
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct and generic way of including the words in the list IMO
1

Try this:

c = df['Country'].str.split(';', expand=True)[0].to_numpy()
df.groupby(c)['Employees'].sum()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.