So let's say I have the following:
Item | Quantity |
---|---|
Blue Banana | 3 |
Red Banana | 4 |
Green Banana | 1 |
Blue Apple | 2 |
Orange Apple | 6 |
I would like to grab all of the bananas and add them, no matter the color. Or I would like to grab all Blue item, no matter the fruit type, and add them.
CodePudding user response:
You can use a dictionary comprehension and str.contains
:
words = ['banana', 'blue']
pd.Series({w: df.loc[df['Item'].str.contains(w, case=False), 'Quantity'].sum()
for w in words})
output:
banana 8
blue 5