0

I want to get the sum of a column based on whether it matches multiple strings. For example,

pd.DataFrame({'key': ['b', 'b d', 'a', 'c t', 'a', 'b p'], 'data1': range(6)})

I want to be able to get the sum for data1 column IF the strings in the key column contain 'b' and 'c'. The sum in this case would be 9.

2
  • df.loc[df['key'].isin(['b','c']), 'data1'].sum() Commented Jan 18, 2021 at 17:27
  • Thank you for the answer. My question was probably not clear so I have edited it for more clarity. Commented Jan 18, 2021 at 17:32

1 Answer 1

1

Try this using regex express and string accessor, .str with contains:

df.loc[df['key'].str.contains('b|c'), 'data1'].sum() 

Output:

9 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.