My dataframe is like,
ID col1
1 Michael Owen
2 Stephen Curry
3 Messi, Lionel
4 James, LeBron
I tried to reverse the order of those names which were split by ", " .
My code is,
df['col1'] = df.col1.str.split().apply(lambda x: ', '.join(x[::-1]))
But it reverses all rows even the names were split by " ".
ID col1
1 Owen, Michael
2 Curry, Stephen
3 Lionel, Messi
4 LeBron, James
Then I tried
df.loc[df['col1'].str.contains(", ").split("col1")].apply(lambda x: ', '.join(x[::-1]))
It gives me an error,
AttributeError: 'Series' object has no attribute 'split'
How can I solve this problem?