Python Pandas : How to convert lists to a dataframe

How to convert lists to a dataframe in python ?

As we know in python, lists are used to store multiple values in an ordered sequence inside a single variable.  Each element inside the list is called an item.

Syntax : my_list = [ element1, element2, element3, .....]

where,

  • elements/items are placed inside square brackets [].
  • items are separated by , symbol.
  • It can contain any number of items.
  • elements can be of different types i.e string, float, integer etc.

The pandas library is one of the most preferred tool to do data manipulation and analysis.

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

Creating DataFrame from list of lists :

Now, let’s take an example.

#program :

import pandas as pd
#List of list
Data=[[‘apple’, ‘banana’, ‘orange’],[‘dog’, ‘cat’, ‘cow’],[‘potato’, ‘tomato’, ‘onion’]]
#creating a dataframe object from list of list
df=pd.DataFrame(Data)
Output:
       0          1               2
0  apple    banana    orange
1  dog        cat           cow
2  potato  tomato    onion

Creating DataFrame from list of tuples :

Now, let’s take an example.

#Program

import pandas as pd
#List of tuples
Data=[(‘apple’, ‘banana’, ‘orange’),(‘dog’, ‘cat’, ‘cow’),(‘potato’, ‘tomato’, ‘onion’)]
#creating a dataframe object from list of tuples
df=pd.DataFrame(Data)
Output:
       0             1                2
0  apple      banana     orange
1  dog          cat           cow
2  potato   tomato     onion

Converting list of tuples to dataframe and set column names and indexes :

We can also make the column and index names.

#Program 


import pandas as pd
#List of tuples 
Data=[(‘apple’, ‘banana’, ‘orange’),(‘dog’, ‘cat’, ‘cow’),(‘potato’, ‘tomato’, ‘onion’)] 
#Converting list of tuples to dataframe and set column names and indexes 
df=pd.DataFrame(Data, columns=[‘a’, ‘b’, ‘c’], index=[‘fruits’, ‘animals’, ‘vegetables’])
Output:
                       0              1                2
fruits             apple     banana      orange
animals          dog        cat           cow
vegetables    potato   tomato     onion

We can also skip one more than one columns like we have 3 rows and 3 columns

This can be used when you don’t need a column

so let’s try removing 1 column.

#Program 

import pandas as pd
#List of tuples 
Data=[(‘apple’, ‘banana’, ‘orange’),(‘dog’, ‘cat’, ‘cow’),(‘potato’, ‘tomato’, ‘onion’)] 
#Converting list of tuples to dataframe and set column names and indexes 
df=pd.DataFrame(Data, exclude=[‘2’], columns=[‘a’, ‘b’, ‘c’], index=[‘fruits’, ‘animals’, ‘vegetables’])
Output:
                       0              1
fruits           apple       banana
animals        dog           cat
vegetables  potato      tomato

Creating a dataframe from multiple lists :

We can also create a dataframe by giving multiple multiple lists.

Let’s try this:

#Program :

import pandas as pd
roll_no = [1, 2, 3]
name = [‘Tia’, ‘Raj’, ‘Rahul’]
state = [‘Goa’, ’Assam’, ‘Punjab’]
wrapoflist = list(zip(roll_no, name, state))
df = pd.DataFrame(wrapoflist, column=[‘roll_no’, ‘name’, ‘state’], index=[‘a’, ‘b’, ‘c’])
Output:
       roll_no  name        state
a        1         Tia           Goa
b        2         Raj          Assam
c        3        Rahul       Punjab

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.