Tai Huang
Tai Huang

Reputation: 35

How to create a Pandas Dataframe from a dictionary with values into one column?

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}

Upvotes: 0

Views: 80

Answers (2)

SeaBean
SeaBean

Reputation: 23217

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}

Upvotes: 2

Andrej Kesely
Andrej Kesely

Reputation: 195543

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}

Upvotes: 1

Related Questions