3

I have some data in this form:

a = [{'table': 'a', 'field':['apple', 'pear']}, 
     {'table': 'b', 'field':['grape', 'berry']}]

I want to create a dataframe that looks like this:

    field table
0   apple     a
1   pear      a
2   grape     b
3   berry     b

When I try this:

pd.DataFrame.from_records(a)

I get this:

            field table
0   [apple, pear]     a
1  [grape, berry]     b

I'm using a loop to restructure my original data, but I think there must be a more straightforward and simpler methid.

Mike Woodward
  • 211
  • 2
  • 10

3 Answers3

4

Option 1
comprehension

pd.DataFrame([{'table': d['table'], 'field': f} for d in a for f in d['field']])

   field table
0  apple     a
1   pear     a
2  grape     b
3  berry     b

Option 2
reconstruct

d1 = pd.DataFrame(a)
pd.DataFrame(dict(
    table=d1.table.repeat(d1.field.str.len()),
    field=np.concatenate(d1.field)
)).reset_index(drop=True)

   field table
0  apple     a
1   pear     a
2  grape     b
3  berry     b

Option 3
Rubik's Cube

pd.DataFrame(a).set_index('table').field.apply(pd.Series) \
    .stack().reset_index('table', name='field').reset_index(drop=True)

  table  field
0     a  apple
1     a   pear
2     b  grape
3     b  berry
piRSquared
  • 285,575
  • 57
  • 475
  • 624
4

You can use a list comprehension to concatenate a series of dataframes, one for each dictionary in a.

>>> pd.concat([pd.DataFrame({'table': d['table'],  # Per @piRSquared for simplification.
                             'field': d['field']})
               for d in a]).reset_index(drop=True)
   field table
0  apple     a
1   pear     a
2  grape     b
3  berry     b
Alexander
  • 105,104
  • 32
  • 201
  • 196
0

Or you can try using pd.wide_to_long ,I do want to use lreshape, but it is undocumented and personally not recommended ...T _ T

a = [{'table': 'a', 'field':['apple', 'pear']},
     {'table': 'b', 'field':['grape', 'berry']}]
df=pd.DataFrame.from_records(a)

df[['Feild1','Feild2']]=df.field.apply(pd.Series)
pd.wide_to_long(df,['Feild'],'table','lol').reset_index().drop('lol',axis=1).sort_values('table')

Out[74]: 
  table  Feild
0     a  apple
2     a   pear
1     b  grape
3     b  berry
BENY
  • 317,841
  • 20
  • 164
  • 234