0

I'm trying to change the values in a single column using pandas apply(). My function partially worked, but I'm stuck on how to fix this other half.

Data Column:

County Name Riverside County San Diego County SanFrancisco County/city

I'm trying to get rid of " County" so I'm left with just the name of the county. I successfully got rid of the " County" with the function but I'm having trouble removing the " County/city" from San Francisco.

Code:

def modify_county(countyname):
  if "/city" in countyname:
    return countyname.replace(" County/city","")
  return countyname.replace(" County","")

lfd["CountyName"] = lfd["CountyName"].apply(modify_county)

Output:

CountyName
Riverside
San Diego
San Francisco County/city

Is something wrong with the conditional in the function?

2
  • Why have an if statement at all? If "/city" isn't in the string, then countyname.replace(" County/city", "") just won't do anything Commented Apr 20, 2018 at 21:53
  • I copied your code and input and it worked fine. Commented Apr 20, 2018 at 22:01

3 Answers 3

1

This is an alternative way. It works with the data you have provided.

import pandas as pd

s = pd.Series(['Riverside County', 'San Diego County', 'SanFrancisco County/city'])

res = s.apply(lambda x: ' '.join([w for w in x.split() if not 'County' in w]))

print(res)

# 0       Riverside
# 1       San Diego
# 2    SanFrancisco
# dtype: object
Sign up to request clarification or add additional context in comments.

Comments

1

@jpp's answer is the literal way of doing what you asked for. But in this case I would use pandas.Series.replace and some regex to substitute the entire thing in one go:

import pandas as pd

s = pd.Series(['Riverside County', 'San Diego County', 'SanFrancisco County/city'])

res = s.replace(' County(/city)?', '', regex=True)

Comments

0

@jpp, used suggestion to apply to entire column. Not sure if this is the best way, but it worked.

lfd["CountyName"] = pd.Series(lfd["CountyName"])

lfd["CountyName"] = lfd["CountyName"].apply(lambda x: ' '.join([w for w in x.split() if not 'County' in w]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.