Python ProgrammingPython Programming

How to Convert Dictionary into DataFrame?

Dictionary to DataFrame:

import pandas as pd

data = ({'Age': [30, 20, 22, 40, 32, 28, 39],
                   'Color': ['Blue', 'Green', 'Red', 'White', 'Gray', 'Black',
                             'Red'],
                   'Food': ['Steak', 'Lamb', 'Mango', 'Apple', 'Cheese',
                            'Melon', 'Beans'],
                   'Height': [165, 70, 120, 80, 180, 172, 150],
                   'Score': [4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2],
                   'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX']
                   })
print(data)

df = pd.DataFrame(data)

print(df)


C:\python\pandas examples>python example15.py
{'Height': [165, 70, 120, 80, 180, 172, 150], 'Food': ['Steak', 'Lamb', 'Mango',
 'Apple', 'Cheese', 'Melon', 'Beans'], 'Age': [30, 20, 22, 40, 32, 28, 39], 'Sco
re': [4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2], 'Color': ['Blue', 'Green', 'Red', 'Whi
te', 'Gray', 'Black', 'Red'], 'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'
]}
   Age  Color    Food  Height  Score State
0   30   Blue   Steak     165    4.6    NY
1   20  Green    Lamb      70    8.3    TX
2   22    Red   Mango     120    9.0    FL
3   40  White   Apple      80    3.3    AL
4   32   Gray  Cheese     180    1.8    AK
5   28  Black   Melon     172    9.5    TX
6   39    Red   Beans     150    2.2    TX
 
C:\python\pandas examples>