In Python, how do you convert a dictionary to a DataFrame?
Let’s create a dictionary.
zoo = dict( [ ['Africa','cheeta, lion, ostrich, baboon'], ['Americas','boa constrictor, tamarin, macaw'] ] ) zoo
Below is the output.
{'Africa': 'cheeta, lion, ostrich, baboon', 'Americas': 'boa constrictor, tamarin, macaw'}
Now we’ll use the DataFrame function of pandas.
import pandas as pd # import pandas # convert the zoo dictionary to a DataFrame df = pd.DataFrame(zoo.items()) df
If you are interested in creating a series from your DataFrame, it’s very easy. Have a look at the post called Pandas Series from DataFrame.