How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?

How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?

Converting a dictionary into a Pandas DataFrame is a common task in data analysis and manipulation. This process involves transforming the key-value pairs of a dictionary into a tabular format, with each key becoming a column and each value becoming a row in the DataFrame. There are several ways to achieve this, including using the built-in “DataFrame” function in Pandas, using the “from_dict” method, or using the “pd.DataFrame.from_records” function. Each of these methods has its own advantages and can be chosen based on the specific needs and structure of the dictionary. By converting a dictionary into a Pandas DataFrame, it becomes possible to utilize the various powerful features and functions of Pandas for further data analysis and manipulation tasks.

Convert Dictionary to Pandas DataFrame (2 Examples)


You can use one of the following methods to convert a dictionary in Python to a pandas DataFrame:

Method 1: Use dict.items()

df = pd.DataFrame(list(some_dict.items()), columns = ['col1', 'col2'])

Method 2: Use from_dict()

df = pd.DataFrame.from_dict(some_dict, orient='index').reset_index()

df.columns = ['col1', 'col2']

Both methods produce the same result.

The following examples show how to use each method in practice.

Example 1: Convert Dictionary to DataFrame Using dict.items()

Suppose we have the following dictionary in Python:

#create dictionary
some_dict = {'Lebron':26,'Luka':30,'Steph':22,'Nicola':29, 'Giannis':31}

We can use the following code to convert this dictionary into a pandas DataFrame:

import pandas as pd

#convert dictionary to pandas DataFrame
df = pd.DataFrame(list(some_dict.items()), columns = ['Player', 'Points'])

#view DataFrame
df

        Player	Points
0	Lebron	26
1	Luka	30
2	Steph	22
3	Nicola	29
4	Giannis	31

We can also use the type() function to confirm that the result is a pandas DataFrame:

#display type of df
type(df)

pandas.core.frame.DataFrame

Example 2: Convert Dictionary to DataFrame Using from_dict()

Suppose we have the following dictionary in Python:

#create dictionary
some_dict = {'Lebron':26,'Luka':30,'Steph':22,'Nicola':29, 'Giannis':31}
import pandas as pd

#convert dictionary to pandas DataFrame
df = pd.DataFrame.from_dict(some_dict, orient='index').reset_index()

#define column names of DataFrame
df.columns = ['Player', 'Points']

#view DataFrame
df

        Player	Points
0	Lebron	26
1	Luka	30
2	Steph	22
3	Nicola	29
4	Giannis	31

We can also use the type() function to confirm that the result is a pandas DataFrame:

#display type of df
type(df)

pandas.core.frame.DataFrame

Notice that this method produces the exact same result as the previous method.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Cite this article

stats writer (2024). How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-dictionary-into-a-pandas-dataframe-what-are-some-ways-to-convert-a-dictionary-to-a-pandas-dataframe/

stats writer. "How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-dictionary-into-a-pandas-dataframe-what-are-some-ways-to-convert-a-dictionary-to-a-pandas-dataframe/.

stats writer. "How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-dictionary-into-a-pandas-dataframe-what-are-some-ways-to-convert-a-dictionary-to-a-pandas-dataframe/.

stats writer (2024) 'How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-dictionary-into-a-pandas-dataframe-what-are-some-ways-to-convert-a-dictionary-to-a-pandas-dataframe/.

[1] stats writer, "How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I convert a dictionary into a Pandas DataFrame?What are some ways to convert a dictionary to a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top