Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 24375

John Ludhi/nbshare.io: How To Convert Python List To Pandas DataFrame

$
0
0

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]:
importpandasaspd

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
0AMC
1GME
2BB
3CLOV
4PLTR

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
0AMC
1GME
2BB
3CLOV
4PLTR

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
0AMC
1GME
2BB
3CLOV
4PLTR

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
0AMC
1GME
2BB
3CLOV
4PLTR
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]:
01234
0AMCGMEBBCLOVPLTR
1AAPLGOOGLAMZNNFLXFB

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]:
01
0AMCAAPL
1GMEGOOGL
2BBAMZN
3CLOVNFLX
4PLTRFB

Now we can rename the columns.

In [67]:
df.columns=['Reddit_stocks','Fang_stocks']
In [68]:
df.head()
Out[68]:
Reddit_stocksFang_stocks
0AMCAAPL
1GMEGOOGL
2BBAMZN
3CLOVNFLX
4PLTRFB

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_stocksFANG_stocks
0AMCGOOGL
1GMEAMZN
2BBAMZN
3CLOVNFLX
4PLTRFB

Viewing all articles
Browse latest Browse all 24375

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>