Home > OS >  I would like to create a df from a dict where 1 columns is dict.keys, and the other is dict values
I would like to create a df from a dict where 1 columns is dict.keys, and the other is dict values

Time:10-16

if a have the dict. =

dict = {'Group1':[3,5,7],'Group2':[78,98,54],'Group3':[100,112,140]}

how to create a df:

value key
3 group1
5 group1
7 group1
78 group2
98 group2
54 group2
100 group3
112 group3
140 group3

CodePudding user response:

You can use the generator directly in the DataFrame constructor

df = pd.DataFrame(
    ((k, v) for k in d for v in d[k]),
    columns=['key', 'value']
)

Output

      key  value
0  Group1      3
1  Group1      5
2  Group1      7
3  Group2     78
4  Group2     98
5  Group2     54
6  Group3    100
7  Group3    112
8  Group3    140

CodePudding user response:

Try:

dct = {"Group1": [3, 5, 7], "Group2": [78, 98, 54], "Group3": [100, 112, 140]}

df = (
    pd.DataFrame(dct)
    .stack()
    .droplevel(0)
    .reset_index()
    .rename(columns={"index": "key", 0: "value"})
    .sort_values("key")
)
print(df)

Prints:

      key  value
0  Group1      3
3  Group1      5
6  Group1      7
7  Group2     54
1  Group2     78
4  Group2     98
2  Group3    100
5  Group3    112
8  Group3    140

CodePudding user response:

You can create a list of lists with values from dct and then create dataframe with the desired column name.

import pandas as pd

dct = {'Group1':[3,5,7],'Group2':[78,98,54],'Group3':[100,112,140]}

res = [[v, key] for key,val in dct.items() for v in val]
# res -> [[3, 'Group1'],[5, 'Group1'],[7, 'Group1'], ... ,[140, 'Group3']]

df = pd.DataFrame(res, columns=['value', 'key'])
print(df)

Output:

   value     key
0      3  Group1
1      5  Group1
2      7  Group1
3     78  Group2
4     98  Group2
5     54  Group2
6    100  Group3
7    112  Group3
8    140  Group3
  • Related