DataFrame from dictionary with nested lists

Evan Bloom

I have a python dictionary with nested lists, that I would like to turn into a pandas DataFrame

a = {'A': [1,2,3], 'B':['a','b','c'],'C':[[1,2],[3,4],[5,6]]}

I would like the final DataFrame to look like this:

> A  B  C
> 1  a  1
> 1  a  2
> 2  b  3
> 2  b  4
> 3  c  5
> 3  c  6

When I use the DataFrame command it looks like this:

pd.DataFrame(a)

>   A   B     C
>0  1   a   [1, 2]
>1  2   b   [3, 4]
>2  3   c   [5, 6]

Is there anyway I make the data long by the elements of C?

Korem

This is what I came up with:

In [53]: df
Out[53]: 
   A  B       C
0  1  a  [1, 2]
1  2  b  [3, 4]
2  3  c  [5, 6]
In [58]: s = df.C.apply(Series).unstack().reset_index(level=0, drop = True)

In [59]: s.name = 'C2'

In [61]: df.drop('C', axis = 1).join(s)
Out[61]: 
   A  B  C2
0  1  a   1
0  1  a   2
1  2  b   3
1  2  b   4
2  3  c   5
2  3  c   6

apply(Series) gives me a DataFrame with two columns. To join them into one while keeping the original index, I use unstack. reset_index removes the first level of the index, which basically holds the index of the value in the original list which was in C. Then I join it back into the df.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

DataFrame from dictionary with nested lists

From Dev

Pandas DataFrame from dictionary with nested lists of dictionaries

From Dev

dataframe from dictionary of lists

From Dev

Pandas DataFrame from Dictionary with Lists

From Dev

Pandas dataframe from nested dictionary

From Dev

Dictionary of lists to nested dictionary

From Dev

creating n nested loops from a dictionary of lists

From Java

creating a Pandas dataframe from a nested dictionary within nested dictionary

From Dev

Converting nested lists to dictionary

From Dev

pandas dataframe from a nested dictionary (elasticsearch result)

From Dev

Create a nested dictionary from pd dataframe Python

From Dev

Pandas dataframe to dictionary of lists

From Dev

Finding the average of columns from nested lists in a dictionary's value

From Dev

How do you create a dictionary from nested lists in Python?

From Dev

Finding the average of columns from nested lists in a dictionary's value

From Dev

How to create a dictionary of lists from two columns in a dataframe

From Dev

Appending to lists stored in a nested dictionary

From Dev

Making a nested dictionary with lists and arrays

From Dev

Convert nested dictionary into dataframe

From Dev

Pandas dataframe to nested dictionary

From Dev

Creating Pandas dataframe from nested unordered html lists

From Java

How to create expanded pandas dataframe from a nested dictionary?

From Dev

Converting a dictionary with lists for values into a dataframe

From Dev

Nested Lists with different lengths into dataframe

From Dev

Nested dictionary of namedtuples to pandas dataframe

From Dev

nested dictionary in list to dataframe python

From Dev

Dual nested dictionary to stacked DataFrame

From Dev

Triple Nested Dictionary to Dataframe in Python

From Dev

Nested dictionary of namedtuples to pandas dataframe

Related Related

  1. 1

    DataFrame from dictionary with nested lists

  2. 2

    Pandas DataFrame from dictionary with nested lists of dictionaries

  3. 3

    dataframe from dictionary of lists

  4. 4

    Pandas DataFrame from Dictionary with Lists

  5. 5

    Pandas dataframe from nested dictionary

  6. 6

    Dictionary of lists to nested dictionary

  7. 7

    creating n nested loops from a dictionary of lists

  8. 8

    creating a Pandas dataframe from a nested dictionary within nested dictionary

  9. 9

    Converting nested lists to dictionary

  10. 10

    pandas dataframe from a nested dictionary (elasticsearch result)

  11. 11

    Create a nested dictionary from pd dataframe Python

  12. 12

    Pandas dataframe to dictionary of lists

  13. 13

    Finding the average of columns from nested lists in a dictionary's value

  14. 14

    How do you create a dictionary from nested lists in Python?

  15. 15

    Finding the average of columns from nested lists in a dictionary's value

  16. 16

    How to create a dictionary of lists from two columns in a dataframe

  17. 17

    Appending to lists stored in a nested dictionary

  18. 18

    Making a nested dictionary with lists and arrays

  19. 19

    Convert nested dictionary into dataframe

  20. 20

    Pandas dataframe to nested dictionary

  21. 21

    Creating Pandas dataframe from nested unordered html lists

  22. 22

    How to create expanded pandas dataframe from a nested dictionary?

  23. 23

    Converting a dictionary with lists for values into a dataframe

  24. 24

    Nested Lists with different lengths into dataframe

  25. 25

    Nested dictionary of namedtuples to pandas dataframe

  26. 26

    nested dictionary in list to dataframe python

  27. 27

    Dual nested dictionary to stacked DataFrame

  28. 28

    Triple Nested Dictionary to Dataframe in Python

  29. 29

    Nested dictionary of namedtuples to pandas dataframe

HotTag

Archive