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'
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.