Create DataFrame from Dictionary
To create a DataFrame from Dictionary in Pandas, pass the dictionary for data
parameter of DataFrame() constructor.
In this tutorial, we will learn how to create a DataFrame from a Python Dictionary, with examples.
Syntax
The syntax to create a DataFrame from Dictionary is
</>
Copy
pandas.DataFrame(data=dictionary)
Examples
DataFrame with Two Columns
In the following program, we create a DataFrame df
, from Python Dictionary d
.
Example.py
</>
Copy
import pandas as pd
d = {'col1': [10, 20, 30], 'col2': [40, 50, 60]}
df = pd.DataFrame(data=d)
print(df)
Output
col1 col2
0 10 40
1 20 50
2 30 60
The keys of dictionary are translated to column names, and the values which are lists are transformed to columns.
DataFrame with Column of Type String
In the following program, we create a DataFrame df
, from Python Dictionary d
, where the first column named name
is of type string.
Example.py
</>
Copy
import pandas as pd
d = {'name': ["apple", "banana", "cherry"], 'quant': [40, 50, 60]}
df = pd.DataFrame(data=d)
print(df)
Output
name quant
0 apple 40
1 banana 50
2 cherry 60
Conclusion
In this Pandas Tutorial, we learned how to a DataFrame from Dictionary in Pandas, using DataFrame() constructor.