How to create Pandas DataFrame from multiple lists?

NumPy is fantastic for numerical data. One can really do powerful operations with numerical data easily and much faster. However, if your data is of mixed type, like some columns are strings while the others are numeric, using data frame with Pandas is the best option.

How to create Pandas DataFrame from lists?

Let us say we have two lists, one of them is of string type and the other is int. We want to make a data frame with these lists as columns.

 months =['Jan','Apr','Mar','June']
 days=[31,30,31,30]

We will see two ways to get data frame from these two list.

Let us make dictionary with two lists.

>>> d = {'Month':months,'Day':days}
>>> d
{'Days': [31, 30, 31, 30], 'Month': ['Jan', 'Apr', 'Mar', 'June']}

Load pandas as pd

import pandas as pd

Create a data frame from using pd.DataFrame function

>>> df = pd.DataFrame(d)
>>> df
   Day Month
0    31   Jan
1    30   Apr
2    31   Mar
3    30  June

Notice that the columns of the data frame is Day first and Month next. Let us we want Month first and Day next in the data frame. To specify the order of the columns, we can use “columns” option with pd.DataFrame like

>>> df = pd.DataFrame(d,columns=['Month','Days'])
>>> df
  Month  Day
0   Jan    31
1   Apr    30
2   Mar    31
3  June    30

Second way to make data frame is to use the zip function. We can use the zipfunction to merge these two lists first.

data_tuples = list(zip(Month,Days))

Then we can call pd.DataFrame on this list of tuples with column names.

>>> pd.DataFrame(data_tuples, columns=['Month','Day'])
 Month Day
0 Jan 31
1 Apr 30
2 Mar 31
3 June 30