I have a question with pandas,
I have the following code to group by State and sum the number of rows that in the String "Measure name" contains the word 'Death',
state_grp = df.groupby(['State'])
state_grp['Measure name'].apply(lambda x: x.str.contains('Death').sum())
Result:
| State | Sum |
|---|---|
| AK | 123 |
| DC | 24 |
but after obtaining the result i want to apply another conditional regarding the acronym of the state, for example if State contains the letter A multiply the 'Sum' by 2.
Final result:
| State | Sum |
|---|---|
| AK | 246 |
| DC | 24 |
I tried again with .apply(lambda x: ..) but I didn't get what I wanted,
I am new to pandas and I want to understand how to do it for study purposes.
After the grouping/summing you can create a boolean mask using
.str.contains()and then use this mask indf.loc[]:Prints:
EDIT: When the result of grouping/summing is series, you can construct the mask slightly differently:
Prints: