Python Forum

Full Version: Sum based on conditions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Everyone,

I am trying to sum a column in a dataframe where the substring of another column is equal to a certain value.

My code is:

sums['Meat'] = data['Imports ($NZD cif)'].sum(axis=1).where(data['Harmonised System Code'].str.slice(0,3) = '2013')
My error note is :

File "<ipython-input-20-4a7a35ac591d>", line 1
sums['Meat'] = data['Imports ($NZD cif)'].sum(axis=1).where(data['Harmonised System Code'].str.slice(0,3) = '2013')
^
SyntaxError: keyword can't be an expression

I don't think it likes the where statement. Anyone know how I go about this?

Thanks for any help :)
Possibly because it's assignment (=) not evaluation (==). Put a second equal sign in there. Check the slice too, I believe you need a 4 instead of 3.
Thanks, I changed my code to the below and think I am closer:

sums['Meat'] = data([data['Harmonised System Code'].str.slice(0,3) == '2013'])[['Imports ($NZD cif)']].sum()
sums.head()
I know get error:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-f5e0ed92d43f> in <module>()
----> 1 sums['Meat'] = data(data['Harmonised System Code'].str.slice(0,3) == '2013')[['Imports ($NZD cif)']].sum()
2 sums.head()

TypeError: 'DataFrame' object is not callable

Any help is appreciated
Thanks
I know little about pandas, but you are getting an error because this is a function call.
data([data['Harmonised System Code'].str.slice(0,3) == '2013'])
I'm guessing "data" is a dataframe, not a function.