0
df = pd.DataFrame(['Ottawa City Council','City of Toronto'], columns = ['Entity'])

How can I change the layout of df to look as below in one step? I know I can do it with loops but I'm seeking the most efficient way as I have 1.2 million rows so it will take hours. Thanks.

enter image description here

Chadee Fouad
  • 2,630
  • 2
  • 23
  • 29

2 Answers2

1

First you have to create the new column, for now with a list of words:

df['Word'] = df.Entity.str.split(' ')

Then explode it (a new function in Pandas version 0.25):

df.explode('Word')
Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
1

df['Word'] = [x.split() for x in df.Entity] df.explode('Word') # pandas >= 25.0

René
  • 4,594
  • 5
  • 23
  • 52