'pandas: split a column on multiple words present in all cells
I would like to split the column in this dataframe on name into two columns,
import pandas as pd
import re
df1 = pd.DataFrame({'Name': ['Steve _ (ICANA) Smith', 'Joe _ (ICANA) Nadal',
'Roger _ (ICANA) Federer_blu']})
My desired output would be:
Name First Last
0 Steve _ (ICANA) Smith Steve Smith
1 Joe _ (ICANA) Nadal Joe Nadal
2 Roger _ (ICANA) Federer_blu Roger Federer_blu
so I would like to get rid of ' _ (ICANA)'. using split, I have done,
df1[['First','last']] = df1.Name.str.split(r"\b _ (ICANA)\b", expand=True)
which returns the following error,
ValueError: Columns must be same length as key
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
