In my DataFrame, I have column named 'teams'. It includes the city and team name. I'd want to extract the city into another column. Here is the dataframe: DataFrame sample
nba_df['team'].head(11)
team
0 Toronto Raptors
1 Boston Celtics
2 Philadelphia 76ers
3 Cleveland Cavaliers
4 Indiana Pacers
5 Miami Heat
6 Milwaukee Bucks
7 Washington Wizards
8 Detroit Pistons
9 Charlotte Hornets
10 New York Knicks
I could easily extract the column using regex:
nba_df['cities'] = nba_df.team.str.extract('(^[\w*]+)', expand=True)
nba_df[['team', 'cities']].head(11)
team cities
0 Toronto Raptors Toronto
1 Boston Celtics Boston
2 Philadelphia 76ers Philadelphia
3 Cleveland Cavaliers Cleveland
4 Indiana Pacers Indiana
5 Miami Heat Miami
6 Milwaukee Bucks Milwaukee
7 Washington Wizards Washington
8 Detroit Pistons Detroit
9 Charlotte Hornets Charlotte
10 New York Knicks New
However, in the column 'names', for New York Knicks, it gives me only the value of: "New" and I'd like to get "New York":
So, how should I do, if the cell has 2words, how can I extract only one word from the beginning and if the cell has 3words, how can I extract 2words from it using regex?