Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain how a dataframe structure can be created using list of dictionary values in Python?
A DataFrame is a two-dimensional data structure where data is stored in tabular format with rows and columns. It can be visualized as an SQL table or Excel sheet representation.
You can create a DataFrame using the following constructor ?
pd.DataFrame(data, index, columns, dtype, copy)
The parameters data, index, columns, dtype, and copy are optional.
Creating DataFrame from List of Dictionaries
When you pass a list of dictionaries to DataFrame, the dictionary keys become column names by default. Each dictionary represents a row of data ?
import pandas as pd
my_data = [
{'ab': 34},
{'mn': 56},
{'gh': 78},
{'wq': 90},
{'az': 123},
{'kl': 45}
]
my_df = pd.DataFrame(my_data)
print("The DataFrame created from list of dictionaries:")
print(my_df)
The DataFrame created from list of dictionaries:
ab az gh kl mn wq
0 34.0 NaN NaN NaN NaN NaN
1 NaN NaN NaN NaN 56.0 NaN
2 NaN NaN 78.0 NaN NaN NaN
3 NaN NaN NaN NaN NaN 90.0
4 NaN 123.0 NaN NaN NaN NaN
5 NaN NaN NaN 45.0 NaN NaN
Better Example with Shared Keys
For more practical use, dictionaries often share common keys ?
import pandas as pd
employees = [
{'name': 'Alice', 'age': 25, 'salary': 50000},
{'name': 'Bob', 'age': 30, 'salary': 60000},
{'name': 'Charlie', 'age': 35, 'salary': 70000}
]
df = pd.DataFrame(employees)
print(df)
name age salary
0 Alice 25 50000
1 Bob 30 60000
2 Charlie 35 70000
How It Works
Each dictionary in the list represents one row of the DataFrame
Dictionary keys become column names automatically
If a key is missing from a dictionary, that cell gets
NaN(Not a Number)All unique keys across all dictionaries become columns
Handling Missing Values
When dictionaries have different keys, pandas fills missing values with NaN ?
import pandas as pd
mixed_data = [
{'name': 'John', 'age': 28},
{'name': 'Jane', 'city': 'New York'},
{'age': 32, 'city': 'Boston', 'salary': 55000}
]
df = pd.DataFrame(mixed_data)
print(df)
name age city salary 0 John 28.0 NaN NaN 1 Jane NaN New York NaN 2 NaN 32.0 Boston 55000.0
Conclusion
Creating DataFrames from lists of dictionaries is intuitive and flexible. Dictionary keys automatically become column names, and missing keys result in NaN values. This approach is ideal when working with structured data from APIs or JSON files.
