How to convert a Python dictionary (dict) to a Pandas dataframe?

Data Science Digest
1 min readMay 24, 2020

--

It is quite easy to convert a dictionary into a dataframe. Simply pass the dict as a parameter to the DataFrame class.

Notice that field1, field2 and field3 are assigned automatically as column names.

If you have a couple of lists, if you want to make a dataframe, you can simply created a dict with “column_name”: list and pass it to DataFrame.

list1 = [...]
list2 = [...]
df = pd.DataFrame({"field1": list1, "field2": list2})

How about a dictionary with key-value pairs. How can we get them to be two columns in the dataframe?

Notice that for this dict, Pandas assign zero for key column and one for value column. In the frame 4 above, we assign column names (name and age) explicitly.

--

--

Data Science Digest

One Digest At a Time. I value your time! #datascience #dataanalyst #datascientist #probability #statistics #ML #AI #savetime #digest