Yicheng Wang
Yicheng Wang

Reputation: 41

How to efficiently construct a dataframe (pandas) from dictionary

The problem is like this:

dict = {0:['A', 'B','C'], 1:'D'}

I want to transfer into the dataframe:

|index|values|
--------------
|  0  |   A  |
--------------
|  0  |   B  |
--------------
|  0  |   C  |
--------------
|  1  |   D  |
--------------

So my idea was to tranform the dictionary into list of tuples:

dict = [(0,'A'), (0,'B'), (0,'C'), (1,'D')]

Then I can create the dataframe i wanted through:

pd.Dataframe(dict)

For the dictionary transformation, what i have been using was:

create a flatten function for list

flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]

use list comprehension to structure the list of tuple

pd.DataFrame(flatten([[(i,jj) for jj in j] for (i, j) in dict.items()]))

Is there a better and more efficient way of solving this problem?

Upvotes: 0

Views: 41

Answers (1)

BENY
BENY

Reputation: 323386

First do not name a dict as dict

Then we look at the question, I am using pd.Series with stack

pd.Series(d).apply(pd.Series).stack().reset_index(level=1,drop=True)
Out[149]: 
0    A
0    B
0    C
1    D
dtype: object

Upvotes: 3

Related Questions