create a dataframe using dictionary code example
Example 1: how to make a pandas dataframe from a dictionary
>>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data)
Example 2: python how to create a pandas dataframe from a dictionary
import pandas as pd
pandas_dataframe = pd.DataFrame(dictionary)
import pandas as pd
student_data = {'name' : ['Jack', 'Riti', 'Aadi'],
'age' : [34, 30, 16],
'city' : ['Sydney', 'Delhi', 'New york']}
pandas_dataframe = pd.DataFrame(student_data)
print(pandas_dataframe)
name age city
0 Jack 34 Sydney
1 Riti 30 Delhi
2 Aadi 16 New york
pandas_dataframe = pd.DataFrame(student_data, columns=['name', 'city'])
print(pandas_dataframe)
name city
0 Jack Sydney
1 Riti Delhi
2 Aadi New york
pandas_dataframe = pd.DataFrame.from_dict(student_data, orient='index')
print(pandas_dataframe)
0 1 2
name Jack Riti Aadi
age 34 30 16
city Sydney Delhi New york
Example 3: create pandas dataframe from dictionary
In [11]: pd.DataFrame(d.items())
Out[11]:
0 1
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391
3 2012-06-28 391
...
In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
Out[12]:
Date DateValue
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391
Example 4: create a dataframe from dict
create a dataframe from dict (function)
import pandas as pd
def create_df():
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
return pd.DataFrame.from_dict(data)
create_df()