29: Program to convert Python dict to dataframe


The conversion of Python dict into dataframe is very simple, to achieve this you will need to make use of the pandas library.

Example:
import pandas as pd

country_capitals_dict = {
    'Country': ['United States', 'Canada', 'Germany', 'France'],
    'Capital': ['Washington D.C.', 'Ottawa', 'Berlin', 'Paris']
}

# Convert dictionary to DataFrame
df = pd.DataFrame(country_capitals_dict)

print(df)
Output:
         Country          Capital

0  United States  Washington D.C.

1         Canada           Ottawa

2        Germany           Berlin

3         France            Paris

4          China          Beijing

5          India        New Delhi

6        Nigeria            Abuja

7   South Africa         Pretoria

References:

Pandas: https://pandas.pydata.org/docs/

-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap