How To Convert Python List To Pandas Dataframe

Pandas dataframe is a very useful data structure.

In this notebook, I will show with examples how to convert Python List to Pandas Dataframe.

In [2]:
import pandas as pd

Convert List to Dataframe

Let us create a dummy list of stock symbols.

In [49]:
stocks = ['AMC', 'GME', 'BB', 'CLOV', 'PLTR']

Creating Dataframe from list can be achieved using pandas.DataFrame.

In [32]:
df = pd.DataFrame(stocks,columns=['ticker'])

Let us look at our dataframe now.

In [33]:
df.head()
Out[33]:
ticker
0 AMC
1 GME
2 BB
3 CLOV
4 PLTR

Notice, the "columns" option in the pd.DataFrame code to name the column name. We can also first create dataframe and then add the column names.

In [50]:
df = pd.DataFrame(stocks)
In [51]:
df.head()
Out[51]:
0
0 AMC
1 GME
2 BB
3 CLOV
4 PLTR

By default, Pandas has named the column 0.

Rename Column Name in Pandas Dataframe

Let us rename the column using dataframe.rename.

In [52]:
df.rename(columns={0: "ticker"},inplace=True)
In [53]:
df.head()
Out[53]:
ticker
0 AMC
1 GME
2 BB
3 CLOV
4 PLTR

Now we can access the column using the column name.

In [13]:
df['ticker']
Out[13]:
0     AMC
1     GME
2      BB
3    CLOV
4    PLTR
Name: ticker, dtype: object

Notice also the index of dataframe. By default, Pandas sets the index starting from 0. We can print the index information using following piece of code.

In [55]:
df.index
Out[55]:
RangeIndex(start=0, stop=5, step=1)

Ofcourse we can use index to access any row value.

In [59]:
df.loc[0]
Out[59]:
ticker    AMC
Name: 0, dtype: object
In [60]:
df.loc[1]
Out[60]:
ticker    GME
Name: 1, dtype: object

To learn more about accessing rows and columns in Pandas Dataframe, check out Select Pandas Dataframe Rows And Columns Using iloc loc and ix

Another way to set or rename the column names in Pandas Dataframe is using dataframe.columns

In [11]:
df.columns = ['ticker']
In [12]:
df.head()
Out[12]:
ticker
0 AMC
1 GME
2 BB
3 CLOV
4 PLTR
In [17]:
df.iloc[0]
Out[17]:
ticker    AMC
Name: 0, dtype: object

How to Convert Python List of Lists to Pandas DataFrame

In the example below, we will convert List of Lists to Dataframe.

Assume we have below list of lists.

In [63]:
stocks = [['AMC', 'GME', 'BB', 'CLOV', 'PLTR'], ['AAPL','GOOGL','AMZN','NFLX','FB']]
In [64]:
pd.DataFrame(stocks)
Out[64]:
0 1 2 3 4
0 AMC GME BB CLOV PLTR
1 AAPL GOOGL AMZN NFLX FB

Notice, our pd.DataFrame command creates the dataframe in wide format. To convert it back to taller format, we can use Pandas dataframe transpose() function.

In [65]:
df = pd.DataFrame(stocks).transpose()
In [66]:
df.head()
Out[66]:
0 1
0 AMC AAPL
1 GME GOOGL
2 BB AMZN
3 CLOV NFLX
4 PLTR FB

Now we can rename the columns.

In [67]:
df.columns = ['Reddit_stocks','Fang_stocks']
In [68]:
df.head()
Out[68]:
Reddit_stocks Fang_stocks
0 AMC AAPL
1 GME GOOGL
2 BB AMZN
3 CLOV NFLX
4 PLTR FB

However Python list of lists could be in following format.

In [70]:
stocks = [['AMC', 'GOOGL'], ['GME', 'AMZN'], ['BB','AMZN'], ['CLOV', 'NFLX'],['PLTR','FB']]

This format is pretty straitforward to convert to Dataframe.

In [71]:
df = pd.DataFrame(stocks,columns=['Reddit_stocks','FANG_stocks'])
In [72]:
df.head()
Out[72]:
Reddit_stocks FANG_stocks
0 AMC GOOGL
1 GME AMZN
2 BB AMZN
3 CLOV NFLX
4 PLTR FB