Practical 2 - DataFrames
DataFrames Introduction
A DataFrame is a Pandas object which represents a table and we can access its columns.
DataFrames are very handy structures; they're designed to be fast and easy to access.
Each column that they contain becomes an attribute of the object that represents the
data frame.
AIM
To Create a dataframe from Lists / Nested Lists
Code #1:
import pandas as pd
suvs = ["scorpio","bolero","safari","fortuner"]
years = ["Y2020","Y2021","Y2022","Y2023"]
price = [ [19.7,16.5,22.5,32.0 ] ,
[19.8,16.7,22.7,32.3 ] ,
[20.1,17.2,23.4,32.5 ] ,
[20.5,17.6,23.9,32.8 ] ]
df = [Link](price, columns = years,index = suvs )
print(df)
Output #1 :
Y2020 Y2021 Y2022 Y2023
scorpio 19.7 16.5 22.5 32.0
bolero 19.8 16.7 22.7 32.3
safari 20.1 17.2 23.4 32.5
fortuner 20.5 17.6 23.9 32.8
AIM
To Create a dataframe from Nested Dictionaries and accessing
individual row / column / value
Code #2:
import pandas as pd
suvs = { "Y2020" : {"bolero":18.5, "scorpio" :22.5 , "safari":28.0 , "fortuner" :35.6 },
"Y2021" : {"bolero":18.6, "scorpio" :22.9 , "safari":28.5 , "fortuner" :36.4 },
"Y2022" : {"bolero":18.9, "scorpio" :23.2 , "safari":29.3 , "fortuner" :36.7 }
df = [Link](suvs)
print(df)
print("-------------------------------------------------")
# access a particular column
print("Data of Year 2021:")
print(df.Y2021) # using dot operator
print("Data of Year 2022:")
print(df['Y2022']) # using square brackets
print("-------------------------------------------------")
# access a particular row
print("scorpio prices over the years")
print([Link]["scorpio",:]) #using loc and row label
print("-------------------------------------------------")
print("safari prices over the years")
print([Link][2,:]) # using iloc and row inedex
print("-------------------------------------------------")
# access a particular value
print("Price of fortuner in Year 2022:",[Link]["fortuner","Y2022"])
print(df)
Output #2 :
Y2020 Y2021 Y2022
bolero 18.5 18.6 18.9
scorpio 22.5 22.9 23.2
safari 28.0 28.5 29.3
fortuner 35.6 36.4 36.7
-------------------------------------------------
Data of Year 2021:
bolero 18.6
scorpio 22.9
safari 28.5
fortuner 36.4
Name: Y2021, dtype: float64
Data of Year 2022:
bolero 18.9
scorpio 23.2
safari 29.3
fortuner 36.7
Name: Y2022, dtype: float64
-------------------------------------------------
scorpio prices over the years
Y2020 22.5
Y2021 22.9
Y2022 23.2
Name: scorpio, dtype: float64
-------------------------------------------------
safari prices over the years
Y2020 28.0
Y2021 28.5
Y2022 29.3
Name: safari, dtype: float64
Price of fortuner in Year 2022: 36.7
AIM
To Create a dataframe from Dictionaries, Series & Lists
Code #3:
import pandas as pd
vehicles = ["safari","bolero","scorpio"]
prices = [15,16,17]
srs_vehicles = [Link](vehicles)
srs_prices = [Link](prices)
map = { "names" : srs_vehicles , "prices" : srs_prices }
#map = { "names" : vehicles , "prices" : prices } #this will
run with lists
df = [Link](map)
print(df)
Output #3 :
names prices
0 safari 15
1 bolero 16
2 scorpio 17