I have a dictionary with tuples as keys and values as lists. I would like to convert it to a pandas dataframe for exporting it as an Excel table later.
Could someone suggest a clean way to achieve the table as below? I would like to have the elements in the list in separate columns.
dict={(a,b):[1,2][4,5],(a,c):[7,8][1,3],(b,c):[1,8][1,3]}
K | X | Y |
---|---|---|
a,b | 1 | 2 |
a,b | 4 | 5 |
a,c | 7 | 8 |
a,c | 1 | 3 |
b,c | 1 | 8 |
b,c | 1 | 3 |
Tried this, not getting desired result:
df=pd.DataFrame.from_dict(dict, orient='index').T
CodePudding user response:
Use:
import pandas as pd
# toy data
dic = {("a", "b"): [[1, 2], [4, 5]], ("a", "c"): [[7, 8], [1, 3]], ("b", "c"): [[1, 8], [1, 3]]}
# un-ravel the data to create the rows of the DataFrame
data = [[key, *value] for key, values in dic.items() for value in values]
# actually create the DataFrame
df = pd.DataFrame(data, columns=["K", "X", "Y"])
print(df)
Output
K X Y
0 (a, b) 1 2
1 (a, b) 4 5
2 (a, c) 7 8
3 (a, c) 1 3
4 (b, c) 1 8
5 (b, c) 1 3
The expression:
data = [[key, *value] for key, values in dic.items() for value in values]
is a list comprehension, is equivalent to:
data = []
for key, values in dic.items():
for value in values:
data.append([key, *value])
CodePudding user response:
Check if this helps:
import pandas as pd
dict={('a','b'):[[1,2],[4,5]],('a','c'):[[7,8],[1,3]],('b','c'):[[1,8],[1,3]]}
dt = []
for k,v in dict.items():
for ls in v:
dt.append([k,ls[0],ls[1]])
df = pd.DataFrame(dt,columns = ['Key','x','y'])
print (df)