In this post, you’ll learn how to create a Pandas dataframe from lists, including how to work with single lists, multiple lists, and lists of lists! You’ll also learn how to work with creating an index and providing column names. Knowing these skills is an important skill when working with data coming from different sources, such as via web scraping.
The Quick Answer: Use the DataFrame() Class to Create Dataframes

Let’s take a look at what you’ll learn!
Table of Contents
The Pandas dataframe() object – A Quick Overview
The pandas Dataframe class is described as a two-dimensional, size-mutable, potentially heterogeneous tabular data. This, in plain-language, means:
two-dimensional
means that it contains rows and columnssize-mutable
means that its size can changepotentially heterogeneous
means that it can contain different datatypes
You can create an empty dataframe by simply writing df = pd.DataFrame()
, which creates an empty dataframe object. We’ve covered creating an empty dataframe before, and how to append data to it. But in this tutorial, you won’t be creating an empty dataframe.
Instead, you can use the data=
parameter, which, positionally is the first argument. The data=
parameter can contain an ndarray
, a dictionary, a list, or a list-like object. Because of these many options, lets see how you can create a dataframe from Pandas lists!
Create a Pandas Dataframe from a Single List
Now that you have an understanding of what the pandas DataFrame
class is, lets take a look at how we can create a Pandas dataframe from a single list.
Recall, that the data=
parameter is the parameter used to pass in data. Because the data=
parameter is the first parameter, we can simply pass in a list without needing to specify the parameter.
Let’s take a look at passing in a single list to create a Pandas dataframe:
import pandas as pdnames = ['Katie', 'Nik', 'James', 'Evan']df = pd.DataFrame(names)print(df)
This returns a dataframe that looks like this:
00 Katie1 Nik2 James3 Evan
Specifying Column Names when Creating a Pandas Dataframe
We can see that Pandas has successfully created our dataframe, but that our column is unnamed. Since Pandas doesn’t actually know what to call the column, we need to more explicit and use the columns=
argument. The columns=
argument takes a list-like object, passing in column headers in the order in which the columns are created.
Let’s re-create our dataframe and specify a column name:
import pandas as pdnames = ['Katie', 'Nik', 'James', 'Evan']df = pd.DataFrame(names, columns=['Name'])print(df)
This now returns a clearly-labelled dataframe that looks like the below:
Name0 Katie1 Nik2 James3 Evan
In the next section, you’ll learn how to create a Pandas dataframe from multiple lists, by using the zip()
function.
Create a Pandas Dataframe from Multiple Lists with Zip
Let’s say you have more than a single list and want to pass them in. Simply passing in multiple lists, unfortunately, doesn’t work. Because of this, we need to combine our lists in order.
The easiest way to do this is to use the built-in zip()
function. The function takes two or more iterables, like lists, and combines them into an object, in the same way that a zipper does!
Let’s see how this can work by creating a Pandas dataframe from two or more lists:
# Create a Pandas Dataframe from Multiple Lists using zip()import pandas as pdnames = ['Katie', 'Nik', 'James', 'Evan']ages = [32, 32, 36, 31]locations = ['London', 'Toronto', 'Atlanta', 'Madrid']zipped = list(zip(names, ages, locations))df = pd.DataFrame(zipped, columns=['Name', 'Age', 'Location'])print(df)
Let’s see what this dataframe looks like by printing it out:
Name Age Location0 Katie 32 London1 Nik 32 Toronto2 James 36 Atlanta3 Evan 31 Madrid
Let’s also break down what we’ve done here:
- We created three lists, containing
names
,ages
, andlocations
, holding our ordered data - We then created a Python
zip()
object, which contained the zips of names, ages, and locations. We then applied thelist()
function to turn this zip object into a list of tuples - We then passed this
zipped
object into ourDataFrame()
class, along with a list of our column names to create our dataframe.
Want to learn more about the zip()
function? Check out my in-depth tutorial on zipping two or more lists in Python and pick up some fun tips and tricks along the way!
In the next section, you’ll learn how to turn lists of lists into a Pandas dataframe!
Create a Pandas Dataframe from a List of Lists
There may be many times you encounter lists of lists, such as when you’re working with web scraping data. Lists of lists are simply lists that contain other lists. They are also often called multi-dimensional lists. For example, a list of lists may look like this:
data = [['Katie', 32, 'London'], ['Nik', 32, 'Toronto']]
Lists of lists behave a little differently, as you’re essentially adding data at, what appears to be, a row level, rather than. column level, as we’ve been exploring so far.
Thankfully, Pandas is intelligent to figure out how to split each list of lists into different columns for you.
Let’s look at how we can create a Pandas dataframe from a list of lists:
# Create a Pandas Dataframe from a multi-dimensional list of listsimport pandas as pddata = [['Katie', 32, 'London'], ['Nik', 32, 'Toronto'], ['James', 36, 'Atlanta'], ['Evan', 31, 'Madrid']]df = pd.DataFrame(data, columns=['Name', 'Age', 'Location'])print(df)
This returns the formatted dataframe below:
Name Age Location0 Katie 32 London1 Nik 32 Toronto2 James 36 Atlanta3 Evan 31 Madrid
In the next section, you’ll learn how to specify datatypes for when creating a Pandas dataframe from a list.
Specifying Data Types with Pandas Dataframes from Lists
While Pandas can do a good job of identifying datatypes, specifying datatypes can have significant performance improvements when loading and maintaining your dataframe. Because of this, it’s an important step to do when you’re either noticing data being loaded incorrectly or you want to manage the memory used by your dataframe.
Let’s take a look at how we can do this in Pandas. We’ll force the age column to be of size int8
, in order to reduce the memory it uses.
import pandas as pddata = [['Katie', 32, 'London'], ['Nik', 32, 'Toronto'], ['James', 36, 'Atlanta'], ['Evan', 31, 'Madrid']]df = pd.DataFrame(data, columns=['Name', 'Age', 'Location'], dtype='int8')print(df)
Now that we’ve specified our Pandas dataframe’s datatypes, let’s take a look at what it looks like:
Name Age Location0 Katie 32 London1 Nik 32 Toronto2 James 36 Atlanta3 Evan 31 Madrid
In the next section, you’ll learn how to create a Pandas dataframe from dictionaries with lists.
Create a Pandas Dataframe from Dictionaries with Lists
In this final section, you’ll learn how to work with dictionaries that contain lists in order to produce a Pandas dataframe. This is something you’ll often encounter when working with web API data, needing to convert complex dictionaries into simplified Pandas dataframes.
Since Pandas does allow for dictionaries to be passed into the data=
parameter, there is little we actually need to do. Let’s see just well Pandas handles dictionaries to create dataframes:
import pandas as pddictionary = { 'Name': ['Katie', 'Nik', 'James', 'Evan'], 'Age': [32, 32, 36, 31], 'Location': ['London', 'Toronto', 'Atlanta', 'Madrid'] }df = pd.DataFrame(dictionary)print(df)
Let’s see what this dataframe looks like now:
Name Age Location0 Katie 32 London1 Nik 32 Toronto2 James 36 Atlanta3 Evan 31 Madrid
Here, we’ve passed in a dictionary that contains lists as the values. Pandas was even able to extrapolate the column names by using the key
values of each item in the dictionary!
Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!
Conclusion
In this post, you learned different ways of creating a Pandas dataframe from lists, including working with a single list, multiple lists with the zip()
function, multi-dimensional lists of lists, and how to apply column names and datatypes to your dataframe.
To learn more about the Pandas dataframe object, check out the official documentation here.
FAQs
How do you make a pandas DataFrame from a list? ›
The pandas DataFrame can be created by using the list of lists, to do this we need to pass a python list of lists as a parameter to the pandas. DataFrame() function. Pandas DataFrame will represent the data in a tabular format, like rows and columns.
How to convert a list of lists into a DataFrame in python? ›Summary: To convert a list of lists into a Pandas DataFrame, use the pd. DataFrame() constructor and pass the list of lists as an argument. An optional columns argument can help you structure the output.
How will you find the top 5 records of a DataFrame in python? ›DataFrame. head() function is used to get the first N rows of Pandas DataFrame. It allows an argument N to the method (which is the first n number of rows we want to get from the start). If the argument is not specified, this function returns the topmost 5 rows from the given DataFrame.
How do I print the first 5 rows of A pandas DataFrame? ›You can use df. head() to get the first N rows in Pandas DataFrame. Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.
How do you write a DataFrame for a list? ›In order to convert Pandas DataFrame to list use df. values. tolist() Here, df. values returns a DataFrame as a NumPy array.
How do you create a DataFrame in Python? ›- import pandas as pd.
- # assign data of lists.
- data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}
- # Create DataFrame.
- df = pd.DataFrame(data)
- # Print the output.
- print(df)
Create Pandas DataFrame from Multiple Lists
Use column param and index param to provide column & row labels respectively to the DataFrame. Alternatively, you can also add column names to DataFrame and set the index using pandas. DataFrame. set_index() method.
A Data frame is simply a List of a specified class called “data. frame”, but the components of the list must be vectors (numeric, character, logical), factors, matrices (numeric), lists, or even other data frames.
How to create a list in Python? ›To create a list in Python, we use square brackets ( [] ). Here's what a list looks like: ListName = [ListItem, ListItem1, ListItem2, ListItem3, ...] Note that lists can have/store different data types.
How can I see the last 5 rows of the DataFrame DF? ›Get last n rows of DataFrame: tail()
The tail() method returns the last n rows. By default, the last 5 rows are returned. You can specify the number of rows.
How do you find the values of a DataFrame in a list? ›
- The top part of the code, contains the syntax to create a DataFrame with the above data.
- The bottom part of the code converts the DataFrame into a list using: df.values.tolist()
Get the First Row of DataFrame using head()
DataFrame. head() method returns the first n rows of dataframe. We can use this head() function to get only the first row of the dataframe, for that, we pass '1' as an argument to this function. It will return the first row of DataFrame.
The head method simply returns the first n rows of your DataFrame. It defaults to the first 5 rows (so actually the 5 in the above code is unnecessary), but you can specify more or less if you'd like.
How do you select the first 3 rows in a DataFrame? ›- Syntax: Dataframe.head(n)
- Syntax: dataframe.iloc[statrt_index, end_index+1]
- Syntax: Dataframe.iloc [ [m,n] ]
We use data. frame() and unlist() functions to create a dataframe using lists. unlist() function is used to covert list to vector so that we can use it as "df" argument in data. frame() function.
Is a Dataframe a list in Python? ›The short answer is No - dataframes are not lists of lists.
How do you create a Dataframe from a list of dictionaries in Python? ›from_records(Data) To create a dataframe from a list of dictionaries in Python, we can also use the from_records() method. The from_records() method takes a list of dictionaries as its input argument and returns a dataframe. Again, the column names for the dataframe consist of the keys in the dictionary.
How do I get DataFrame data in Python? ›The info() method prints information about the DataFrame. The information contains the number of columns, column labels, column data types, memory usage, range index, and the number of cells in each column (non-null values). Note: the info() method actually prints the info.
How to create a dataset in Python? ›- To create a dataset for a classification problem with python, we use the make_classification method available in the sci-kit learn library. ...
- The make_classification method returns by default, ndarrays which corresponds to the variable/feature and the target/output.
You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame.
How do you create a DataFrame from two columns of another data frame? ›
Using DataFrame.copy() Create New DataFrame
Select the columns from the original DataFrame and copy it to create a new DataFrame using copy() function. Yields below output. Alternatively, You can also use DataFrame.filter() method to create a copy and create a new DataFrame by selecting specific columns.
tolist() you can convert pandas DataFrame Column to List. df['Courses'] returns the DataFrame column as a Series and then use values. tolist() to convert the column values to list.
What is data Frame example? ›What is a DataFrame? A DataFrame is a data structure that organizes data into a 2-dimensional table of rows and columns, much like a spreadsheet. DataFrames are one of the most common data structures used in modern data analytics because they are a flexible and intuitive way of storing and working with data.
What is the difference between list and DataFrame in python? ›A DataFrame is a data type in python. This data type is constructed of multiple values in a structure defined by user parameters. Lists are limited by structure. Arrays are a value constructed with multiple values to create a new entity, but restricted to numbers only.
Can you have a list in a DataFrame? ›You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame. loc() methods. Each of these method takes different arguments, in this article I will explain how to use insert the list into the cell by using these methods with examples.
What is list () in Python? ›The list() function creates a list object. A list object is a collection which is ordered and changeable.
How to get data from list in Python? ›List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0. (See the official python.org list docs.)
What is list in Python with example? ›List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
How to convert pandas series to DataFrame? ›...
Syntax
- The passed name should substitute for the series name (if it has one).
- The fault is None.
- Returns the DataFrame representation of Series.
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.
How do I convert a nested list to a DataFrame? ›
Converting the lists to a DataFrame
To create a DataFrame, we will first assign the newly created list to pd. DataFrame and assign column name as 'station'. We will also add a column that contains the station addresses.
To create a DataFrame with this list of tuples, we will simply use pandas. DataFrame() method inside which we will pass a list of tuples, but we have to pass a parameter called columns=[] for which we will assign a list of column headers.
How do you convert a dataset into a DataFrame? ›- from sklearn import datasets.
- import pandas as pd.
- boston_data = datasets. load_boston()
- df_boston = pd. DataFrame(boston_data. data, columns=boston_data. feature_names)
- df_boston['target'] = pd. Series(boston_data. target)
- df_boston. head()
You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame. This takes several params, for our scenario we use list that takes series to combine and axis=1 to specify merge series as columns instead of rows.
How do I change DataFrame data type in pandas? ›- Use astype() to force an appropriate dtype.
- Create a custom function to convert the data.
- Use pandas functions such as to_numeric() or to_datetime()
from_records(Data) To create a dataframe from a list of dictionaries in Python, we can also use the from_records() method. The from_records() method takes a list of dictionaries as its input argument and returns a dataframe. Again, the column names for the dataframe consist of the keys in the dictionary.
How do I create a DataFrame from two lists in python? ›Create Pandas DataFrame from Multiple Lists
Use column param and index param to provide column & row labels respectively to the DataFrame. Alternatively, you can also add column names to DataFrame and set the index using pandas. DataFrame. set_index() method.
Method 1: Create DataFrame from Dictionary using default Constructor of pandas. Dataframe class. Method 2: Create DataFrame from Dictionary with user-defined indexes. Method 3: Create DataFrame from simple dictionary i.e dictionary with key and simple value like integer or string value.
Can we convert list to Dataframe in Python? ›- Using DataFrame()
- Using list with index and column names.
- Using zip()
- Using Multidimensional list.
- Using multidimensional list with column and data type.
- Using lists in the dictionary.
tolist() to get a list of a specified column. From the dataframe, we select the column “Name” using a [] operator that returns a Series object. Next, we will use the function Series. to_list() provided by the Series class to convert the series object and return a list.
Can you create a Pandas series from a list? ›
If you have a list of lists and you can easily convert it to a pandas Series by iterating over them using for loop and pass it into Series() function, it will return a series based on its index.
How do I add a column to a Pandas DataFrame from a list in Python? ›Adding a column using Pandas Series
You can simply assign the values of your Series into the existing DataFrame to add a new column: series = pd. Series([40, 38, 32.5, 27, 30], index=[0, 1, 2, 3, 4])