-2
  1. I'm trying to go through each row in column 'birth'
  2. Check if the last part of the string separated by "," ends in two characters 2.a. If it does, I will append "US" to it.

So, "Los Angeles, Ca" would be "Los Angeles, Ca, US" And "Bisacquino, Sicily, Italy" would stay the same

I want to process this in a function.

I've tried this but when checking checking the length of birthStr it gives me the length of all the rows

for row in subset.itertuples():        
   birthStr= subset['birth'].str.rsplit(",", 1).str[-1]
   if len(birthStr) ==2:
      subset.birth = birthStr + "," + "US"
Hah
  • 3
  • 1
  • you should not iterate over the dataframe using `itertuples`, it should be a last case resort – gold_cy Mar 28 '19 at 20:01
  • Add some sample data and expected output to this question. May like 5 to 10 examples mixed with those that need US and those that don't. – Scott Boston Mar 28 '19 at 20:03
  • In addition to what @aws_apprentice stated, check what is returned by `itertuples()`, and how many input variables you're iterating over in your function – G. Anderson Mar 28 '19 at 20:15
  • see [this post](https://stackoverflow.com/a/20159305/2327328) about making good pandas examples – philshem Mar 28 '19 at 21:41

1 Answers1

0

We can use the str methods provided by pandas to solve for this. Let's use the following dataframe that I define below.

print(df)
                       place
0            Los Angeles, Ca
1  Bisacquino, Sicily, Italy
2               New York, NY


condition = df.place.str.split(',').str[-1].str.strip().str.len() == 2

df.loc[condition, 'place'] = df.place + ', US'

print(df)

                       place
0        Los Angeles, Ca, US
1  Bisacquino, Sicily, Italy
2           New York, NY, US
gold_cy
  • 13,648
  • 3
  • 23
  • 45