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.

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)

         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


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

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

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap