10

Under Python3, I have a dict with the following format:

my_dict = {'col1': 1.0, 'col2':2.0, 'col3': 3.0}

And I want to convert it to a pandas DataFrame using dict keys as columns:

      col1  col2  col3
0     1.0   2.0   3.0

However, when I try the following command I have a ValueError:

df = pd.DataFrame(my_dict)

ValueError: If using all scalar values, you must pass an index
Pierre S.
  • 988
  • 1
  • 14
  • 20

1 Answers1

13

Use:

df = pd.DataFrame([my_dict])

Or:

df = pd.DataFrame.from_dict(my_dict, orient='index').T

Or:

df = pd.DataFrame(my_dict, index=[0])

print (df)
   col1  col2  col3
0   1.0   2.0   3.0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252