Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame from list/dict/list

Tags:

python

pandas

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.

like image 646
Mike Woodward Avatar asked Jun 06 '26 15:06

Mike Woodward


2 Answers

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
like image 94
Alexander Avatar answered Jun 09 '26 03:06

Alexander


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
like image 25
piRSquared Avatar answered Jun 09 '26 03:06

piRSquared



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!