Convert Dictionary to DataFrame in Python with column names
Last Updated :
28 Apr, 2025
In Python, the pandas library provides a powerful and flexible tool for working with tabular data through its DataFrame class. When creating a DataFrame from a dictionary, it's common to set the keys of the dictionary as the column names of the resulting DataFrame. In this article, we will see how we can create a Pandas DataFrame from a dictionary as keys as column names.
Python DataFrame from Dictionary as Keys as Column Name
Below are some of the ways to achieve this task in Python:
- Using pd.DataFrame() Method
- Using list() method
- Using Series() method
Using pd.DataFrame() Method
In this example, a Python dictionary students
containing names as keys and corresponding marks as values is converted to a Pandas DataFrame using pd.DataFrame()
. The resulting DataFrame df
displays the names and marks in tabular form with columns labeled 'Name' and 'Marks'.
Python3
import pandas as pd
students = {"Shravan": 90, "Jeetu": 91, "Ram": 32, "Pankaj": 95}
# Convert the dictionary to a Pandas DataFrame
df = pd.DataFrame(students.items(), columns=['Name', 'Marks'])
print(df)
Output:
Name Marks
0 Shravan 90
1 Jeetu 91
2 Ram 32
3 Pankaj 95
Using list() method
In this example, a Python dictionary students
with names as keys and corresponding marks as values is converted into a Pandas DataFrame using pd.DataFrame(list(students.items()), columns=['Name', 'Marks'])
. The resulting DataFrame df
presents the names and marks in tabular form with columns labeled 'Name' and 'Marks'.
Python3
import pandas as pd
students = {"Shravan": 90, "Jeetu": 91, "Ram": 32, "Pankaj": 95}
# Convert dictionary to DataFrame
df = pd.DataFrame(list(students.items()), columns=['Name', 'Marks'])
print(df)
Output:
Name Marks
0 Shravan 90
1 Jeetu 91
2 Ram 32
3 Pankaj 95
Using Series() method
In this example, a Python dictionary students
with names as keys and corresponding scores as values is initially converted to a Pandas Series using pd.Series(students, name='Score')
. Subsequently, the series is transformed into a DataFrame df
with column names 'Name' and 'Score' by resetting the index and renaming the columns accordingly.
Python3
import pandas as pd
students = {"Shravan": 90, "Jeetu": 91, "Ram": 32, "Pankaj": 95}
# Convert dictionary to Series
series = pd.Series(students, name='Score')
# Convert series to DataFrame
df = pd.DataFrame(series)
df.reset_index(inplace=True)
df.columns = ['Name', 'Score']
print(df)
Output:
Name Score
0 Shravan 90
1 Jeetu 91
2 Ram 32
3 Pankaj 95
Similar Reads
How to convert tab-separated file into a dataframe using Python In this article, we will learn how to convert a TSV file into a data frame using Python and the Pandas library. A TSV (Tab-Separated Values) file is a plain text file where data is organized in rows and columns, with each column separated by a tab character. It is a type of delimiter-separated file,
4 min read
Dynamically Rename Multiple Columns in PySpark DataFrame In this article, we are going to learn how to dynamically rename multiple columns in Pyspark data frame in Python. A data frame that is equivalent to a relational table in Spark SQL, and can be created using various functions in SparkSession is known as Pyspark data frame. While working in Pyspark,
13 min read
Convert Dict of List to CSV - Python To convert a dictionary of lists to a CSV file in Python, we need to transform the dictionary's structure into a tabular format that is suitable for CSV output. A dictionary of lists typically consists of keys that represent column names and corresponding lists that represent column data.For example
3 min read
Convert PySpark DataFrame to Dictionary in Python In this article, we are going to see how to convert the PySpark data frame to the dictionary, where keys are column names and values are column values. Before starting, we will create a sample Dataframe: Python3 # Importing necessary libraries from pyspark.sql import SparkSession # Create a spark se
3 min read
Convert list to dataframe with specific column names in R A list contains different types of objects as their components. The components may belong to different data types or different dimensions. Vector can be useful components of a list and can be easily mapped as the rows or columns of a dataframe. Each column in the dataframe is referenced using a uniq
4 min read
Convert Row Names into Column of DataFrame in R In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which
3 min read