remove from , and transfer to next cell in pandas

Viewed 28

remove from , and transfer to next cell in pandas for eg,

city country
Toronto,Canada N/A

output:

city country
Toronto,Canada Canada
2 Answers

If need replace missing value in country column by last value after split city by , use:

df['country'] = df['country'].fillna(df['city'].str.split(',').str[-1])

Or if need assign all column in country column:

df['country'] = df['city'].str.split(',').str[-1]

You can use str.extract with a word regex \w+ anchored to the end of the string ($) to get the last word:

# replacing all values
df['country'] = df['city'].str.extract('(\w+)$', expand=False)

# only updating NaNs
df.loc[df['country'].isna(), 'country'] = df['city'].str.extract('(\w+)$', expand=False)

output:

             city country
0  Toronto,Canada  Canada

Alternatively, you can use the ([^,]+)$ regex that is more permissive (any terminal character except ,)

Related