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
  • 1,975
  • 1
  • 19
  • 21

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
  • 27,886
  • 3
  • 21
  • 36
1

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

Rene
  • 3,062
  • 3
  • 17
  • 41