1

I have dictionary like below:

dict = {key_1:[[1, 2], [3, 4]], key_2:[[1, 2], [3, 4]]}

I want to convert this to a dataframe like below:

      colum_1 column_2 
key_1   1       2 
key_1   3       4 
key_2   1       2 
key_2   3       4 

What is the most efficient way to do this. Thanks for help=)

3 Answers 3

4

Let us try comprehension to unnest the key-val pairs

pd.DataFrame((k, *l) for k, v in d.items() for l in v).set_index(0)

       1  2
0          
key_1  1  2
key_1  3  4
key_2  1  2
key_2  3  4
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one @Shubham ;)
2

IIUC, you could use:

cols = ['col1', 'col2']
df = pd.DataFrame({k: zip(*v) for k,v in d.items()}, index=cols).T.explode(cols)

output:

      col1 col2
key_1    1    2
key_1    3    4
key_2    1    2
key_2    3    4

Comments

1

Using pandas methods

Here is a pure pandas way of doing this without using any list/dict comprehensions for anyone looking for this -

d = {"key_1":[[1, 2], [3, 4]], "key_2":[[1, 2], [3, 4]]}
df = pd.DataFrame(d).T.stack().droplevel(-1).apply(pd.Series)
print(df)
       0  1
key_1  1  2
key_1  3  4
key_2  1  2
key_2  3  4

Benchmarks -

%%timeit
pd.DataFrame(d).T.stack().droplevel(-1).apply(pd.Series)

100 loops, best of 5: 2.56 ms per loop

%%timeit
pd.DataFrame((k, *l) for k, v in d.items() for l in v).set_index(0)

1000 loops, best of 5: 719 µs per loop

%%timeit
cols = ['col1', 'col2']
pd.DataFrame({k: zip(*v) for k,v in d.items()}, index=cols).T.explode(cols)

100 loops, best of 5: 6.53 ms per loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.