Python Forum
Sum with substr in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum with substr in Python
#1
Hi everyone,

I am a SAS user and it has a very useful function call substr. I cannot find an equivalent for Pandas or Python.

I am trying to sum a variable where another string variables first two letters = '08'

my attempt is below:

data.loc[data.Hs_code[0:1] == '08'].Imports_vfd.sum()
My error:TypeError: invalid type comparison

Any help is appreciated
Thanks
Reply
#2
Note that SAS's substr(text, 1, 2) is equivalent to text[0:2] in Python, or more simply text[:2]. But since strings are sequences of characters in Python, and dataframe columns are sequences in pandas, You are really getting a slice of the column Hs_code.

The way around this is to use the str method of the column:

data.loc[data.Hs_code.str[:2] == '08'].Imports_vfd.sum()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I tried that and got this error:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas
Reply
#4
Your column or series Hs_code has not the type object
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Substr on Pandas Dataframe Scott 1 2,569 Sep-02-2019, 02:49 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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