Andrew Hamel
Andrew Hamel

Reputation: 366

Turn a dictionary with one key and multiple values into a dataframe

I have a dictionary comprised of one key and multiple values.

dict = {'key1':['value1','value2']}

I would to take this dictionary and turn it into a pandas dataframe in the following form:

column1    column2
key1    value1
key1    value2

Where for every value, key is indicated.

Thank you!

Upvotes: 2

Views: 1266

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Try:

dct = {'key1':['value1','value2']}
pd.DataFrame(dct).melt()

Output:

  variable   value
0     key1  value1
1     key1  value2

Upvotes: 3

Quang Hoang
Quang Hoang

Reputation: 150745

How about pd.DataFrame(my_dict).stack()?

Upvotes: 1

Related Questions