Create a Pandas DataFrame from List of Dicts
To convert your list of dicts to a pandas dataframe use the following methods:- pd.DataFrame(data)
- pd.DataFrame.from_dict(data)
- pd.DataFrame.from_records(data)
Create List of Dictionaries
data = [
{'Name': 'John', 'TotalMarks': 82, 'Grade': 'A', 'Promoted': True},
{'Name': 'Doe', 'TotalMarks': 38, 'Grade':'E', 'Promoted': False},
{'Name':'Bill', 'TotalMarks': 63, 'Grade': 'B', 'Promoted': True}]
List of dictionaries to a pandas DataFrame
df = pd.DataFrame(data)
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
from_dict() method
df = pd.DataFrame.from_dict(data)
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
Dictionary Orientations
There are two primary types of dictionary orientations : "columns" , and "index" . So, it is important to make the distinction between the different types of dictionary orientations. Dictionaries with the orient='columns' will have their keys correspond to columns in the equivalent DataFrame.
df = pd.DataFrame.from_dict(data, orient='columns')
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
from_records() method
df = pd.DataFrame.from_records(data)
df
Name TotalMarks Grade Promoted
0 John 82 A True
1 Doe 38 E False
2 Bill 63 B True
Setting Custom Index
If you want to set custom index you can use index parameter .
df = pd.DataFrame.from_records(data,index=['1', '2', '3'])
df
Name TotalMarks Grade Promoted
1 John 82 A True
2 Doe 38 E False
3 Bill 63 B True
Related Topics
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to delete column from pandas DataFrame
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- How to set a particular cell value in pandas DataFrame