Home > Software design >  How to create a Pandas Dataframe from a dictionary with values into one column?
How to create a Pandas Dataframe from a dictionary with values into one column?

Time:09-17

Suppose dict = {'A':{1,2,4}, 'B':{5,6}}, How to create a Pandas Dataframe like this:

    Key  Value
0   'A'  {1,2,4}
1   'B'  {5,6}

CodePudding user response:

You can feed the dict to pd.Series and then convert the series to dataframe with reset_index(), as follows:

d = {'A':{1,2,4}, 'B':{5,6}}

df = pd.Series(d).rename_axis(index='Key').reset_index(name='Value')

Result:

print(df)

  Key      Value
0   A  {1, 2, 4}
1   B     {5, 6}

CodePudding user response:

Try:

dct = {"A": {1, 2, 4}, "B": {5, 6}}

df = pd.DataFrame({"Key": dct.keys(), "Value": dct.values()})
print(df)

Prints:

  Key      Value
0   A  {1, 2, 4}
1   B     {5, 6}
  • Related