The Wayback Machine - https://web.archive.org/web/20240717152233/https://www.geeksforgeeks.org/create-a-pandas-dataframe-from-lists/
Open In App

Create a Pandas DataFrame from Lists

Last Updated : 26 Jun, 2024
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease.

List to Dataframe Example

# Simple list
data = [1, 2, 3, 4, 5]

# Convert to DataFrame
df = pd.DataFrame(data, columns=['Numbers'])

Here we will discuss different ways to create a Pandas Dataframe from the lists:

Create Dataframe from List using Constructer

To convert a list to a Pandas DataFrame, you can use the pd.DataFrame() constructor. This function takes a list as input and creates a DataFrame with the same number of rows and columns as the input list.

Python
# import pandas as pd
import pandas as pd

# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is', 
            'portal', 'for', 'Geeks']

# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
print(df)

Output:

 0
0 Geeks
1 For
2 Geeks
3 is
4 portal
5 for
6 Geeks

Create DataFrame from List using Dictionary

To use lists in a dictionary to create a Pandas DataFrame, we Create a dictionary of lists and then Pass the dictionary to the pd.DataFrame() constructor. Optionally, we can specify the column names for the DataFrame by passing a list of strings to the columns parameter of the pd.DataFrame() constructor.

Python
# importing pandas as pd 
import pandas as pd 
 
# list of name, degree, score
nme = ["aparna", "pankaj", "sudhir", "Geeku"]
deg = ["MBA", "BCA", "M.Tech", "MBA"]
scr = [90, 40, 80, 98]
 
# dictionary of lists 
dict = {'name': nme, 'degree': deg, 'score': scr} 
   
df = pd.DataFrame(dict)
   
print(df) 

Output:

 name  degree  score
0 aparna MBA 90
1 pankaj BCA 40
2 sudhir M.Tech 80
3 Geeku MBA 98

Convert List to Pandas Dataframe using zip()

To create a Pandas DataFrame from lists using zip(). We can also use the zip() function to zip together multiple lists to create a DataFrame with more columns.

Python
# import pandas as pd
import pandas as pd

# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']

# list of int
lst2 = [11, 22, 33, 44, 55, 66, 77]

# Calling DataFrame constructor after zipping
# both lists, with columns specified
df = pd.DataFrame(list(zip(lst, lst2)),
               columns =['Name', 'val'])
print(df)

Output:

Name  val
0 Geeks 11
1 For 22
2 Geeks 33
3 is 44
4 portal 55
5 for 66
6 Geeks 77

Create DataFrame from List by changing data type

To create a Pandas DataFrame using a multi-dimensional list with column names and dtypes specified. By specifying dtypes, we can ensure that the DataFrame is created with the correct data types.

Python
import pandas as pd

# List1 
lst = [['tom', 'reacher', 25], ['krish', 'pete', 30],
       ['nick', 'wilson', 26], ['juli', 'williams', 22]]

# Create DataFrame
df = pd.DataFrame(lst, columns=['FName', 'LName', 'Age'])

# Convert 'Age' column to float
df['Age'] = df['Age'].astype(float)

print(df)

Output:

   FName     LName   Age
0 tom reacher 25.0
1 krish pete 30.0
2 nick wilson 26.0
3 juli williams 22.0

Create DataFrame from List using multi-dimensional list

To create a DataFrame using a multi-dimensional list, you can use the pd.DataFrame() constructor. The pd.DataFrame() constructor takes a list of lists as input and creates a DataFrame with the same number of rows and columns as the input list.

Python
# import pandas as pd
import pandas as pd 
  
# List1 
lst = [['tom', 25], ['krish', 30],
       ['nick', 26], ['juli', 22]]
  
df = pd.DataFrame(lst, columns =['Name', 'Age'])
print(df)

Output:

Name  Age
0 tom 25
1 krish 30
2 nick 26
3 juli 22

Create DataFrame from List with index and column names

To create a DataFrame using a list with index and column names, you can use the pd.DataFrame() constructor with the index and columns parameters.

Python
# import pandas as pd
import pandas as pd

# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']

# Calling DataFrame constructor on list
# with indices and columns specified
df = pd.DataFrame(lst, index =['a', 'b', 'c', 'd', 'e', 'f', 'g'],
                                              columns =['Names'])
print(df)

Output:

Names
a Geeks
b For
c Geeks
d is
e portal
f for
g Geeks

Convert List to Dataframe – FAQs

How to convert a list to a pandas DataFrame?

You can convert a Python list to a Pandas DataFrame using the ‘pd.DataFrame()’ constructor.

import pandas as pd

my_list = [1, 2, 3, 4, 5]
df = pd.DataFrame(my_list, columns=['Numbers'])

print(df)

How to make a pandas DataFrame a list?

To convert a Pandas DataFrame to a Python list, you can access the DataFrame values and convert them to a list using ‘.tolist()’.

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
my_list = df.values.tolist()

print(my_list)

How to create a pandas DataFrame from a list of tuples?

You can create a DataFrame from a list of tuples using the ‘pd.DataFrame()’ constructor and specifying column names.

import pandas as pd

data = [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
df = pd.DataFrame(data, columns=['Name', 'Age'])

print(df)

How to convert a nested list to a DataFrame?

To convert a nested list (list of lists) to a DataFrame, you can directly pass it to ‘pd.DataFrame()’.

import pandas as pd

nested_list = [[1, 'Alice', 25], [2, 'Bob', 30], [3, 'Charlie', 35]]
df = pd.DataFrame(nested_list, columns=['ID', 'Name', 'Age'])

print(df)

How to list DataFrames into one DataFrame in Pandas?

If you have multiple DataFrames and want to concatenate them row-wise into one DataFrame, you can use ‘pd.concat()’.

import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

df_combined = pd.concat([df1, df2], ignore_index=True)

print(df_combined)


Previous Article
Next Article

Similar Reads

Create pandas dataframe from lists using zip
One of the way to create Pandas DataFrame is by using zip() function. You can use the lists to create lists of tuples and create a dictionary from it. Then, this dictionary can be used to construct a dataframe. zip() function creates the objects and that can be used to produce single item at a time. This function can create pandas DataFrames by mer
2 min read
Python | Create a Pandas Dataframe from a dict of equal length lists
Given a dictionary of equal length lists, task is to create a Pandas DataFrame from it. There are various ways of creating a DataFrame in Pandas. One way is to convert a dictionary containing lists of equal lengths as values. Let's discuss how to create a Pandas Dataframe from a dict of equal length lists with help of examples. Example #1: Given a
2 min read
Create pandas dataframe from lists using dictionary
Pandas DataFrame is a 2-dimensional labeled data structure like any table with rows and columns. The size and values of the dataframe are mutable, i.e., can be modified. It is the most commonly used pandas object. Creating pandas data-frame from lists using dictionary can be achieved in multiple ways. Let’s discuss different ways to create a DataFr
2 min read
Pandas DataFrame assign() Method | Create new Columns in DataFrame
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, making importing and analyzing data much easier. The Dataframe.assign() method assigns new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original one
4 min read
Pandas DataFrame hist() Method | Create Histogram in Pandas
A histogram is a graphical representation of the numerical data. Sometimes you'll want to share data insights with someone, and using graphical representations has become the industry standard. Pandas.DataFrame.hist() function plots the histogram of a given Data frame. It is useful in understanding the distribution of numeric variables. This functi
4 min read
Creating Pandas dataframe using list of lists
In this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are various methods for Creating a Pandas data frame us
4 min read
Difference Between Spark DataFrame and Pandas DataFrame
Dataframe represents a table of data with rows and columns, Dataframe concepts never change in any Programming language, however, Spark Dataframe and Pandas Dataframe are quite different. In this article, we are going to see the difference between Spark dataframe and Pandas Dataframe. Pandas DataFrame Pandas is an open-source Python library based o
3 min read
Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). This data structure can be converted to NumPy ndarray with the help of the DataFrame.to_numpy() method. In this article we will see how to convert dataframe to numpy array. Syntax of Pandas DataFrame.to_numpy()
3 min read
Convert given Pandas series into a dataframe with its index as another column on the dataframe
First of all, let we understand that what are pandas series. Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor. Syntax: pandas.Series(data, index, dtype, copy) Return: Series object.
1 min read
How to Convert Wide Dataframe to Tidy Dataframe with Pandas stack()?
We might sometimes need a tidy/long-form of data for data analysis. So, in python's library Pandas there are a few ways to reshape a dataframe which is in wide form into a dataframe in long/tidy form. Here, we will discuss converting data from a wide form into a long-form using the pandas function stack(). stack() mainly stacks the specified index
4 min read
three90RightbarBannerImg