Create DataFrame from list

DataFrame can be created using a single list or a list of lists.

In [1]:
#Import library
import numpy as np
import pandas as pd
In [2]:
#Define data with the help of list
data = [['joe',10],['Andy',18],['Ricky',28]]
In [4]:
#Create DataFrame
df = pd.DataFrame(data,columns = ['Name','Age'])
Name Age
0 joe 10
1 Andy 18
2 Ricky 28

Create a DataFrame with or without index.

All the ndarrays must be of same length. If index is passed, then the length of the index should equal to the length of the arrays.

If no index is passed, then by default, index will be range(n), where n is the array length.

In [6]:
#Here no index passed
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print (df)
    Name  Age
0    Tom   28
1   Jack   34
2  Steve   29
3  Ricky   42
In [8]:
#create an indexed DataFrame using arrays.
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print (df)
        Name  Age
rank1    Tom   28
rank2   Jack   34
rank3  Steve   29
rank4  Ricky   42

Create a DataFrame from list of dictonary

List of dictonary passed as a input data to create a DataFrame. The dictonary key are by default taken as column name.

In [9]:
data = [{'a': 10, 'b': 20},{'a': 55, 'b': 100, 'c': 200}]
df = pd.DataFrame(data)
print (df)
    a    b      c
0  10   20    NaN
1  55  100  200.0

We will learn how to read diffrent file in Pandas in next chepter