To convert a dictionary into a Pandas DataFrame, you can use the pd.DataFrame()
function. The keys of the dictionary will serve as column names or index labels, depending on the structure of the data. The way you convert it depends on the structure of your dictionary.
In this article, I will explain how to convert a dictionary into a DataFrame using Pandas, emphasizing different methods and techniques depending on the dictionary’s structure. I will also discuss essential features, including how to use the orient
parameter to define various output formats.
Key Points –
- The structure of the dictionary (e.g., lists, dictionaries, tuples) determines how the DataFrame is created. Each type requires a specific approach.
- By default,
pd.DataFrame()
treats dictionary keys as column names. Useorient='index'
to treat keys as row indices. - A common use case is converting a dictionary where each key maps to a list, with each list becoming a column.
- When keys map to other dictionaries, the outer keys become the DataFrame’s index, and inner keys are used as columns.
- Using
pd.DataFrame.from_dict
()
method allows explicit control over orientation, making it useful for complex structures. - When using dictionaries where values are tuples or lists, specify column names during conversion to ensure clarity.
Convert a Dictionary Into a DataFrame
To convert a dictionary into a Pandas DataFrame, you can use the pd.DataFrame()
function. Pandas allows you to create a DataFrame from dictionaries with different types of structures, such as dictionaries of lists, dictionaries of dictionaries, etc.
Dictionary with Lists as Values
To create a DataFrame from a dictionary where each key corresponds to a list of values, you can directly use the pd.DataFrame()
constructor. Each key will become a column in the DataFrame, and the lists will represent the data for those columns.
import pandas as pd
# Example dictionary
data = {
'Courses': ["Spark", "PySpark", "Hadoop", "Pandas"],
'Fee': [22000, 25000, 24000, 26000],
'Duration': ['30days', '50days', '40days', '60days'],
'Discount': [1000, 2300, 2500, 1400]
}
# Convert to DataFrame
df = pd.DataFrame(data)
print("DataFrame from Dictionary with Lists as Values:\n", df)
Here,
- Each key in the dictionary becomes a column in the DataFrame. Each list under the keys contains the data for that column.
- By default, Pandas assigns a default integer index to each row (0, 1, 2, …).
- This structure is useful for representing tabular data in a concise and readable format.
Converting Dictionary to DataFrame With Orient=‘Index’
Alternatively, to convert a dictionary to a DataFrame using orient='index'
, you specify that the dictionary keys should be treated as the DataFrame’s index labels, and the values should become rows.
To convert a dictionary where the values are themselves dictionaries, you can use the pd.DataFrame.from_dict()
method with the orient='index'
parameter. This will treat the outer dictionary’s keys as the row indices, and the inner dictionary’s keys as the column names.
import pandas as pd
# Example dictionary
data = {
'peter': {'Course': 'Spark','Fee': 25000, 'Duration': '30days'},
'James': {'Course': 'Pyspark', 'Fee': 30000, 'Duration': '40days'},
'Charlie': {'Course': 'Pandas', 'Fee': 35000, 'Duration': '45days'}
}
# Convert to DataFrame with keys as index
df = pd.DataFrame.from_dict(data, orient='index')
print("DataFrame from Dictionary of Dictionaries (Orient as Index):\n", df)
Here,
- The
orient='index'
argument treats the outer keys of the dictionary as the DataFrame’s row indices. - The inner dictionary keys (
'Course'
,'Fee'
,'Duration'
) become column labels, and their corresponding values become the DataFrame’s cell values. - Each corresponding value from the inner dictionary fills the appropriate cell in the DataFrame.
Dictionary with Lists as Keys (Orient as Index)
When converting a dictionary to a DataFrame where each key is associated with a list, you can orient the DataFrame so that the keys become row indices using the orient='index'
parameter.
import pandas as pd
# Example dictionary
data = {
'peter': ['Spark', 22000],
'James': [ 'Pandas', 25000],
'Charlie': ['Pyspark', 30000]
}
# Convert to DataFrame with keys as index
df = pd.DataFrame.from_dict(data, orient='index', columns=['Courses', 'Fee',])
print("DataFrame from Dictionary with Lists as Keys (Orient as Index):\n", df)
# Output:
# DataFrame from Dictionary with Lists as Keys (Orient as Index):
# Courses Fee
# peter Spark 22000
# James Pandas 25000
# Charlie Pyspark 30000
Here,
- The keys (
'peter'
,'James'
,'Charlie'
) become the index (row labels) of the DataFrame. - The lists associated with each key become the rows.
- You can manually specify the column names using the columns parameter.
Convert Dictionary of Series
To convert a dictionary of Pandas Series to a DataFrame, you can directly use the pd.DataFrame()
constructor. Each Series will become a column in the DataFrame.
import pandas as pd
# Example dictionary of Series
data = {
'Courses': pd.Series(['Spark', 'Pyspark', 'Pandas']),
'Fee': pd.Series([25000, 30000, 35000]),
'Duration': pd.Series(['30days', '50days', '40days'])
}
# Convert to DataFrame
df = pd.DataFrame(data)
print("DataFrame from Dictionary of Series:\n", df)
# Output:
# DataFrame from Dictionary of Series:
# Courses Fee Duration
# 0 Spark 25000 30days
# 1 Pyspark 30000 50days
# 2 Pandas 35000 40days
Here,
- The keys in the dictionary (
'Courses'
,'Fee'
, and'Duration'
) become the column names of the DataFrame. - Each Series associated with a key provides the data for that column.
- Pandas automatically assigns an integer index to the rows (0, 1, 2, …).
FAQ on Pandas Convert Dictionary to DataFrame
Use pd.DataFrame()
directly. The keys will become column names, and the lists will form the data rows.
Use pd.DataFrame.from_dict(your_dict, orient='index')
. This treats the outer keys as index labels, and the inner dictionaries as rows.
The orient
parameter determines how the data is interpreted. 'index'
treats the keys as row labels, while 'columns'
(default) treats them as column names.
By passing a columns
parameter to pd.DataFrame()
or renaming the columns after creation using df.columns
.
Use pd.DataFrame.from_dict(your_dict, orient='index')
and customize the DataFrame by splitting or handling tuple keys if needed.
Conclusion
In conclusion, converting a dictionary to a DataFrame in Pandas is a flexible and powerful way to organize and analyze data. Depending on the structure of the dictionary. Also learned when we pass the param orient
how it can be specified in the output format.
Happy Learning!!
Related Articles
- Pandas Convert DataFrame to Dictionary (Dict)
- Convert Pandas Index to List
- Pandas Convert JSON to DataFrame
- Pandas Convert Column to Numpy Array
- Pandas Convert String to Integer
- How to Convert Pandas DataFrame to List?
- Pandas Convert Integer to String in DataFrame
- Pandas Convert List of Dictionaries to DataFrame
- Pandas Convert Floats to Strings in DataFrame
- Pandas Convert Boolean to String in DataFrame
- Pandas Convert Boolean to String in DataFrame
- Convert Multiple Columns to String in Pandas DataFrame