Grade XII Informatics Practices
Chapter: 1 Data Handling using Pandas -II
Practice assignment – Different methods of DataFrame creation
Note: For ease of understanding these notes, the following colour coding is followed:
All code lines: Green. In code wherever >>> is there it is interactive mode, otherwise script mode.
All comments : Red colour
All outputs are given in box in blue colour
1. Creation of Empty dataframe Empty DataFrame
import pandas as pd1 Columns: []
df1 = [Link]() Index: []
print(df1)
2. Creation of Dataframe with List 0
import pandas as pd1 0 1
data1 = [1,2,3,4,5] 1 2
df1 = [Link](data1) 2 3
print (df1) 3 4
4 5
3. Dataframe from List with data and column index value
Name Age
import pandas as pd1
0 Freya 10
data1 = [['Freya',10],['Mohak',12],['Dwivedi',13]]
1 Mohak 12
df1 = [Link](data1,columns=['Name','Age'])
2 Dwivedi 13
print (df1)
print(df1['Name']) # retrieving values column wise
print(df1['Age'])
4. Creation of Dataframe with List Data value with float
Name Age
import pandas as pd1
0 Freya 10.0
data1 = [['Freya',10],['Mohak',12],['Dwivedi',13]]
1 Mohak 12.0
df1 = [Link](data1,columns=['Name','Age'],dtype=float)
2 Dwivedi 13.0
print (df1)
# here numeric values dtype changes to float
5. Creation of dataframe from dict and list
import pandas as pd1 Name Age
data1 = {'Name':['Freya', 'Mohak'],'Age':[9,10]} 0 Freya 9
df1 = [Link](data1) 1 Mohak 10
print (df1)
# keys of dictionary becomes the columns of dataframe
6. Creation of a DataFrame from List of Dicts
x y z
import pandas as pd1
0 1 2 NaN
data1 = [ {'x': 1, 'y': 2}, {'x': 5, 'y': 4, 'z': 5}]
1 5 4 5.0
df1 = [Link](data1)
print (df1)
# note that the keys will be taken as column index
# to give row index, index attribute can be used with DataFrame()
# df1 = [Link](data1, index=['first', 'second'])
7. Creation of a DataFrame from csv(comma separated value) file / import data from cvs file
# save comma separated values in a csv file in the same folder as python program.
[Link]
Taken from first row of csv file
import pandas as pd1
Date price factor_1 factor_2
data = pd1.read_csv("[Link]")
0 2012-06-11 1600.20 1.255 1.548
df1 = [Link](data)
1 2012-06-12 1610.02 1.258 1.554
print (df1)
8. Creating a DataFrame from .txt file
# save some data in a txt file in the same folder as python program.
[Link]
A B C
import pandas as pd1 0 1 1 12.92
data = pd1.read_table('[Link]',names=('A', 'B', 'C')) 1 1 2 90.75
df1 = [Link](data) 2 1 3 60.90
print(df1) 3 2 1 71.34
*********************************************