简体   繁体   中英

Python Pandas: Create DataFrame from dictionary that has values of list of lists

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=)

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM