Reputation: 1998
I am looking to tweak the code below:
df.loc[df['Range'] == '6-10','Actual'].sum()
If I want to add another column called city, to this where would I alter this?
For example if Range column value is '6-10' then I want sum of Actual column as above in code, but I want to tweak that code so I can also filter of the City column value is 'NY' for example.
Thanks!
Upvotes: 0
Views: 56
Reputation: 1897
You can do it two ways:
df.loc[(df['Range'] == '6-10') & (df['City'] == 'NY'),'Actual'].sum()
Or another which might be more extensible
query_str = "(Range == '6-10') and (City == 'NY')"
df.query(query_str)['Actual'].sum()
Upvotes: 2
Reputation: 323276
You can do
df.loc[df['Range'].eq('6-10')&df['City'].eq('NY'),'Actual'].sum()
Upvotes: 1