Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum based on conditions
#1
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 :)
Larz60+ write Jan-11-2021, 09:41 AM:
Please, Always post entire unmodified error traceback in error tags.
there is important information within that will help with debugging.
Reply
#2
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.
buran likes this post
Reply
#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
Reply
#4
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020