Convering dictionary into dataframe pandas python

Post Reply
programmer
Posts: 3
Joined: 27 Mar 2019

Convering dictionary into dataframe pandas python

Post by programmer » 27 Mar 2019

Hello, I am new to pandas and I have written a code to store different values in the form of key and values pairs. Please have a look at the below code.

Code: Select all

countries_details = {'name': ['India', 'South Africa', 'Australia'],
'population in crores': [133, 6, 33],
'Best Place to Visit': ['Taj Mahal','Boulders Beach','MANLY BEACH']}
print(countries_details)
I want to convert this dictionary into dataframe. Can you please guide me how to apporach for it.

User avatar
Aisangam
Site Admin
Posts: 7
Joined: 24 Mar 2019
Location: Delhi, India
Contact:

Re: Convering dictionary into dataframe pandas python

Post by Aisangam » 27 Mar 2019

As I could look into the code, you have made the dictionary which holds both key and value. Data frame will take these keys as the headers and values as the rows. I hope you will also visit http://www.aisangam.com/blog/quick-intr ... dataframe/ to get used to basics of python. Now coming back to the code. Let me write down your code again for you as down

Code: Select all

countries_details = {'name': ['India', 'South Africa', 'Australia'],
'population in crores': [133, 6, 33],
'Best Place to Visit': ['Taj Mahal','Boulders Beach','MANLY BEACH']}
countries_details is your dictionary and just pass follow the below code to convert it into dataframe

Code: Select all

import pandas as pd
df = pd.DataFrame(countries_details)
print("Dataframe from dictionary is",df)
If you face any problem in importing pandas, you can install it using the below command

Code: Select all

pip install pandas

Post Reply