pang2016
pang2016

Reputation: 539

pandas unstack the list

I encounter a problem:

import pandas
data=pandas.DataFrame({'data1':[[('m',2)],[('n',3),('y',4)],[('x',3),('y',5)],[('m',3)]]},
       index=[['a','a','c','d'],[1,1,3,4]])

the data like this:

        data1
a   1   [(m, 2)]
    1   [(n, 3), (y, 4)]
c   3   [(x, 3), (y, 5)]
d   4   [(m, 3)]

I wanted the result like this:

       key  value
a   1   m   2
    1   n   3
    1   y   4
c   3   x   3
    3   y   5
d   4   m   3

thx!

Upvotes: 1

Views: 294

Answers (1)

jezrael
jezrael

Reputation: 863196

You can use list comprehension for creating df by tuples and then reshape by stack:

df = pd.DataFrame([dict(x) for x in data.data1], index=data.index)
print (df)
       m    n    x    y
a 1  2.0  NaN  NaN  NaN
  1  NaN  3.0  NaN  4.0
c 3  NaN  NaN  3.0  5.0
d 4  3.0  NaN  NaN  NaN

df = df.stack().astype(int).reset_index(level=2)
df.columns = ['key','value']
print (df)
    key  value
a 1   m      2
  1   n      3
  1   y      4
c 3   x      3
  3   y      5
d 4   m      3

Upvotes: 1

Related Questions