Introduction
Python’s Pandas library is a powerful tool for data manipulation and analysis. One common task is converting Pandas DataFrame to dictionary, which allows for easy integration with other Python data structures. In this article, we will delve into various techniques for converting DataFrames to dictionaries, providing seven advanced examples along with detailed explanations and output.
9 Examples explaining the conversion of Pandas Dataframe to Dictionary
Example 1: Basic Conversion of Pandas Dataframe to Dictionary
import pandas as pd # Create a sample DataFrame data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]} df = pd.DataFrame(data) # Convert DataFrame to dictionary dictionary = df.to_dict() print(dictionary)
Explanation:
- We start by importing the
pandas
library and creating a simple DataFrame with two columns: ‘Name’ and ‘Age’. - The
df.to_dict()
function is used to convert the DataFrame into a dictionary. - The resulting dictionary will have column names as keys and lists of values as corresponding values.
Output:
{'Name': {0: 'John', 1: 'Jane', 2: 'Jim'}, 'Age': {0: 30, 1: 25, 2: 35}}
Example 2: Orient=’list’
data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]} df = pd.DataFrame(data) # Convert DataFrame to list of dictionaries list_of_dicts = df.to_dict(orient='list') print(list_of_dicts)
Explanation:
- In this example, we use the
orient='list'
argument when callingdf.to_dict()
. - This results in a dictionary where each column is represented as a list.
Output:
{'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]}
Example 3: Index as Keys
data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]} df = pd.DataFrame(data) # Convert DataFrame to dictionary with index as keys dictionary = df.set_index('Name').to_dict()['Age'] print(dictionary)
Explanation:
- Here, we first set the ‘Name’ column as the index using
df.set_index('Name')
. - Then, we use
to_dict()['Age']
to get a dictionary where ‘Name’ is the key and ‘Age’ is the value.
Output:
{'John': 30, 'Jane': 25, 'Jim': 35}
Example 4: Nested Python Dictionary
data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]} df = pd.DataFrame(data) # Convert DataFrame to a nested dictionary dictionary = df.set_index('Name').to_dict(orient='index') print(dictionary)
Explanation:
- In this example, we use
to_dict(orient='index')
to create a nested dictionary where the outer keys are the index values.
Output:
{'John': {'Age': 30}, 'Jane': {'Age': 25}, 'Jim': {'Age': 35}}
Example 5: Using ‘split’ Orient
data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]} df = pd.DataFrame(data) # Convert DataFrame to dictionary with 'split' orient dictionary = df.to_dict(orient='split') print(dictionary)
Explanation:
- By using
orient='split'
, we get a dictionary containing'columns'
,'data'
, and'index'
keys.
Output:
{'index': [0, 1, 2], 'columns': ['Name', 'Age'], 'data': [['John', 30], ['Jane', 25], ['Jim', 35]]}
Example 6: Applying Custom Functions
data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35]} df = pd.DataFrame(data) # Apply a custom function to convert DataFrame to dictionary def custom_converter(row): return {'Name': row['Name'], 'Double_Age': row['Age'] * 2} dictionary = df.apply(custom_converter, axis=1, result_type='expand').to_dict(orient='records') print(dictionary)
Explanation:
- Here, we define a custom function
custom_converter
that takes a row of the DataFrame and returns a dictionary with custom keys and values. - We use
df.apply()
to apply this function to each row. result_type='expand'
ensures that the output is a DataFrame.- Finally, we convert the DataFrame to a list of dictionaries using
to_dict(orient='records')
.
Output:
[{'Name': 'John', 'Double_Age': 60}, {'Name': 'Jane', 'Double_Age': 50}, {'Name': 'Jim', 'Double_Age': 70}]
Example 7: Handling Missing Values
# Create a sample DataFrame with missing values data = {'Name': ['John', 'Jane', 'Jim', None], 'Age': [30, 25, 35, None]} df = pd.DataFrame(data) # Convert DataFrame to dictionary, handling missing values dictionary = df.fillna('N/A').to_dict() print(dictionary)
Explanation:
- This example demonstrates handling missing values by using
fillna()
to replace them with a specified value (‘N/A’ in this case) before converting to a dictionary.
Output:
{'Name': {0: 'John', 1: 'Jane', 2: 'Jim', 3: 'N/A'}, 'Age': {0: 30.0, 1: 25.0, 2: 35.0, 3: 'N/A'}}
Example 8: Hierarchical Indexing
# Create a sample DataFrame with hierarchical index data = {'Value': [10, 20, 30, 40, 50, 60]} index = pd.MultiIndex.from_product([['A', 'B'], [1, 2, 3]], names=['Letter', 'Number']) df = pd.DataFrame(data, index=index) # Convert DataFrame to dictionary with hierarchical index dictionary = df.to_dict()['Value'] print(dictionary)
Explanation:
- In this example, we create a DataFrame with a hierarchical index using
pd.MultiIndex.from_product()
. - The DataFrame has two levels of index: ‘Letter’ and ‘Number’.
- We then use
df.to_dict()['Value']
to get a dictionary where the hierarchical index is preserved.
Output:
{('A', 1): 10, ('A', 2): 20, ('A', 3): 30, ('B', 1): 40, ('B', 2): 50, ('B', 3): 60}
Example 9: Customized Dictionary Structure
# Create a sample DataFrame data = {'Name': ['John', 'Jane', 'Jim'], 'Age': [30, 25, 35], 'City': ['New York', 'San Francisco', 'Seattle']} df = pd.DataFrame(data) # Convert DataFrame to a customized dictionary structure def custom_converter(row): return {'Person Info': {'Name': row['Name'], 'Age': row['Age']}, 'Location': {'City': row['City']}} dictionary = df.apply(custom_converter, axis=1, result_type='expand').to_dict(orient='records') print(dictionary)
Explanation:
- In this example, we define a custom function
custom_converter
that takes a row of the DataFrame and returns a dictionary with a customized structure. - The resulting dictionary has two main keys:
'Person Info'
and'Location'
, each containing sub-dictionaries. - We use
df.apply()
to apply this function to each row, andresult_type='expand'
to ensure the output is a DataFrame. - Finally, we convert the DataFrame to a list of dictionaries using
to_dict(orient='records')
.
Output:
[{'Person Info': {'Name': 'John', 'Age': 30}, 'Location': {'City': 'New York'}}, {'Person Info': {'Name': 'Jane', 'Age': 25}, 'Location': {'City': 'San Francisco'}}, {'Person Info': {'Name': 'Jim', 'Age': 35}, 'Location': {'City': 'Seattle'}}]
Conclusion
In this article, we’ve explored various techniques for converting Python Pandas DataFrame to dictionary. These examples showcase different scenarios and provide a solid foundation for handling more complex data conversion tasks. With this valuable information, you can efficiently integrate DataFrames with other Python data structures, enhancing your data analysis capabilities. You can visit my other articles on Pandas Dataframes as well for more in-depth information.
Thank you for reading it.