I have a column in a df like so:
city
Rockford, IL
St. Petersburg
Oklahoma City
Memphis
Indianapolis
Naples, FL
I want to know how to remove the comma and following characters from elements in the column that have it. so new one like this:
city
Rockford
St. Petersburg
Oklahoma City
Memphis
Indianapolis
Naples
I know I can rename each element individually. But I'd like a way to do this at once for the whole column. Thanks!
CodePudding user response:
Using str.replace
we can try:
df["city"] = df["city"].str.replace(r',[^,]*$', '')
CodePudding user response:
You can also split on comma and take the first element using str.split.str[0]
,
df['city'] = df['city'].str.split(',').str[0]
Output:
city
0 Rockford
1 St. Petersburg
2 Oklahoma City
3 Memphis
4 Indianapolis
5 Naples