Create Data Frame Using List Dictionary Array

In this class, We discuss how to create a data frame using a list dictionary array.

For Complete YouTube Video: Click Here

Create Data Frame

The reader should have prior knowledge of the data frame. Click here.

Take an example and understand how to create a data frame using a list dictionary and array.

Data Frames using the list

In the panda’s module, we have a class called a data frame. This class is used to create a data frame using the list.

The sample program is given below.

# create data frame using lists
import pandas as pd
lst=["hello","how","are","you"]
df=pd.DataFrame(data=lst)
print(df)     #  default index will be 0,1,2,3....

Output:
       0
0  hello
1    how
2    are
3    you

In the program, we defined a list, and the list is given to the data frame class to the parameter data.

We defined the data frame class. So it is creating a data frame object with the data from the list.

The variable df is referencing the data frame.

The output is shown in the program above.

By default, the index is created, and the column names are given from zero.

Change Index of the Data Frame

We use the parameter index to change the index of the data frame.

The example program is given below.

#how to change index of data frame
lst=["hello","how","are","you"]
df=pd.DataFrame(data=lst,index=['a','b','c','d'])
print(df) 

Output:
       0
a  hello
b    how
c    are
d    you

The index values are taken a,b,c,d.

Change Column Names

To change the column names, we use the parameter columns.

The column name is given firstcol.

# Changing column names
lst=["hello","how","are","you"]
df=pd.DataFrame(data=lst,index=['a','b','c','d'],columns=['firstcol'])
print(df)  

Output:
  firstcol
a    hello
b      how
c      are
d      you

Data Frame using list within the list

The example is shown below.

# create data frame using list with in list
lst=[[1,'suresh'],[2,'harish'],[3,'suraj']]
df=pd.DataFrame(data=lst,index=['a','b','c'],columns=['id','name'])
print(df)

Output:
   id    name
a   1  suresh
b   2  harish
c   3   suraj

Each list in the list is considered as a row in the data frame.

The inner list contains two elements. So the data frame has two columns.

Data Frame using Dictionaries

The example was shown below.

# Data Frame using Dictionaries
students={'name':['rajesh','suresh','mahesh','j'],'age':[25,35,40,1],'marks':[85,45,65,20]}
df=pd.DataFrame(data=students)
print(df)

Output:
     name  age  marks
0  rajesh   25     85
1  suresh   35     45
2  mahesh   40     65
3       j    1     20

Each key in the dictionary is taken as a column in the data frame.

The value of each key is a list. And the list is considered as elements in the column.

The output of the data frame is shown above.

Data Frame using Numpy Arrays

A two-dimensional numpy array has been created, and the array is given as input to the data frame.

The two-dimensional is converted to a table in the data frame.

The code and the output are shown below.

# Data Frame using numpy arrays
import pandas as pd
import numpy as np
array=np.array([[1,1,1],[2,2,2],[5,5,5]])
df=pd.DataFrame(data=array)
print(df)

Output:
   0  1  2
0  1  1  1
1  2  2  2
2  5  5  5