0

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

email                      addresses

[email protected]         [{'street': 'a'}, {'street': 'b'}]
[email protected]      [{'street': 'c'}]

And I expect this result:

email                         street

[email protected]            'a'
[email protected]            'b'
[email protected]         '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     [email protected]      a
0     [email protected]      b
1  [email protected]      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