0

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?

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Yeah, dict should never be named for dictionary.
Although this approach seems to be slower than my original approach.@Wen

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.