0

This is my original dataframe. Every row has an email and a list of addresses (There is just street to exemplify).

email                      addresses

somename@gmail.com         [{'street': 'a'}, {'street': 'b'}]
anothername@gmail.com      [{'street': 'c'}]

And I expect this result:

email                         street

somename@gmail.com            'a'
somename@gmail.com            'b'
anothername@gmail.com         'c'

Is there a better way in pandas than iterate over the array to create this last dataframe?

  • https://stackoverflow.com/questions/32468402/how-to-explode-a-list-inside-a-dataframe-cell-into-separate-rows – Ami Tavory Mar 13 '19 at 19:11

1 Answers1

1

You can use:

df1=pd.DataFrame({'email':df.email.repeat(df.addresses.str.len()),\
                   'addresses':np.concatenate(df.addresses.values)})
df1['street']=df1.pop('addresses').apply(pd.Series)
print(df1)

                   email street
0     somename@gmail.com      a
0     somename@gmail.com      b
1  anothername@gmail.com      c
anky
  • 74,114
  • 11
  • 41
  • 70
  • 1
    This is a really nice elegant solution! `df.addresses.str.len()` grabs the length of the dicts rowwise, didnt know. – Erfan Mar 13 '19 at 19:17