0

I need to extract the values from a column in pandas df and save those values in a separate column. I need to grab the strings that follow a specific pattern. My data looks like this:

name        
ADV | FashionWeek.ab
ADV | FashionWeek
ADV | ESPN.arb
ADV | ESPN.ob
ADV | ESPN

The desired output is:

name                        clean_name  
ADV | FashionWeek.ab       FashionWeek
ADV | FashionWeek          FashionWeek
ADV | ESPN.arb             ESPN
ADV | ESPN.ob              ESPN
ADV | ESPN                 ESPN

So I have to grab everything that comes after ADV | and before . I tried:

new_df["clean_name"] = df["name"].split('|')[1].lstrip().split('.')[0]

error: AttributeError: 'DataFrame' object has no attribute 'split'

1 Answer 1

2

You have the string logic right, you were just applying the split function to the dataframe object and not the items inside the column. To do this, you can use the .apply function:

df["clean_name"] = df["name"].apply(lambda x: x.split('|')[1].lstrip().split('.')[0])
Sign up to request clarification or add additional context in comments.

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.