Pandas List To DataFrame – How To Create

You might need to convert your numpy arrays into a DataFrame. Or maybe you have a list of values (while working in vanilla python) and you want to turn them into a DataFrame within Pandas. That is where list to dataframe comes into play.

pandas.DataFrame(Your_list_or_lists)

I do this most often when I’m iterating through a for loop, saving the values, and then need to create a new DataFrame from them. Important: When passing multiple lists, you must pass lists that represent rows.

Pseudo Code: With your python lists, turn them into a pandas DataFrame

Pandas DataFrame

Pandas List To DataFrame - How to create a new dataframe from a list or lists of lists

Pandas List To DataFrame Parameters

Since creating a DataFrame from a list uses the standard pd.DataFrame(), you only have to worry about those parameters

  • data = The data that you want your DataFrame to be created from. In this case, it is going to be the list(s) you pass. Each list represents a row in your future DataFrame.
  • index = You can pass an index that is the same length as the number of lists you pass. Think of index as row labels
  • columns = Your column names. The length of this list of columns must match the number of items in your child lists.

Now the fun part, let’s take a look at a code sample

In [4]:
import pandas as pd

Pandas List To DataFrame

You may want to create a DataFrame from a list or list of lists. In this case, all you need to do is call the general pd.DataFrame() function and pass your data.

We will run through 3 examples:

  1. Creating a DataFrame from a single list
  2. Creating a DataFrame from multiple lists
  3. Creating a DataFrame from multiple lists and specifying column names
  4. Creating a DataFrame from multiple lists and specifying column names and index

But first, let's create some lists

In [8]:
list1 = ["Foreign Cinema", 50]
list2 = ["Liho Liho", 45]
list3 = ["500 Club", 102]
list4 = ["The Square", 65]

1. Creating a DataFrame from a single list

To start off, let's create a DataFrame from a single list. Do do this I'm going to call pd.DataFrame, then pass data=my_list.

You can see, when I pass one list, pandas returns a single column DataFrame. The list values are the row within a single column.

In [12]:
pd.DataFrame(data=list1)
Out[12]:
0
0Foreign Cinema
150

2. Creating a DataFrame from multiple lists

If I wanted to create a DataFrame with multiple rows (I do this most of the time), then you just need to pass a list of lists in to data

In [13]:
pd.DataFrame(data=[list1, list2, list3, list4])
Out[13]:
01
0Foreign Cinema50
1Liho Liho45
2500 Club102
3The Square65

3. Creating a DataFrame from multiple lists and specifying column names

Next, let's specify column names. Pass your column names to the column parameter. Make sure that the number of columns you specify equals the number of columns in your DataFrame.

In [14]:
pd.DataFrame(data=[list1, list2, list3, list4], columns=['Name', 'num_customers'])
Out[14]:
Namenum_customers
0Foreign Cinema50
1Liho Liho45
2500 Club102
3The Square65

4. Creating a DataFrame from multiple lists and specifying column names and index

Finally, let's create a DataFrame with a specific index. To do this I'll pass a list of index labels to the index parameter.

In [15]:
pd.DataFrame(data=[list1, list2, list3, list4],
             columns=['Name', 'num_customers'],
             index=['res1', 'res2', 'res3', 'res4'])
Out[15]:
Namenum_customers
res1Foreign Cinema50
res2Liho Liho45
res3500 Club102
res4The Square65

Link to code above

Check out more Pandas functions on our Pandas Page

Official Documentation