Packages (pandas &
matplotlib)
1
User defined
functions
o Functions are common to all programming languages defined
as a block of re-usable code to perform specific tasks.
o User-defined functions are written by the developers to meet
certain requirements.
2
Creating function
def multi_or_sum(num1, num2):
num3=num1 * num2
if(num3 < 1000):
return num3
else:
return num1+num2
number1 = int(input("Enter first number "))
number2 = int(input("Enter second number"))
result = multi_or_sum(number1, number2)
print(result)
3
pandas
• pandas is one of data centric python packages made for
data manipulation.
• Using pandas you can directly load csv, html, json, txt and
other file formats into python and handle them.
4
pandas
Reading data from Automobile_data.csv file and display
first 5 and last 5 rows:
import pandas as pd
df = pd.read_csv("C:\\Python27\\Automobile_data.csv")
print([Link](5))
print([Link](5))
5
pandas
Reading data from Automobile_data.csv file and sorting according to price:
import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
new_df = df.sort_values("price")
print(new_df)
How to sort price in ascending order?
How to remove null values?
6
pandas
Find the most expensive car company name
new_df = df [['company','price']][[Link]==df['price'].max()]
Find the car with highest horsepower?
7
pandas
Find cars with horsepower more than 200
new_df = df [['company',horsepower']][[Link] > 200]
8
Visualizations with
matplotlib
9
matplotlib
Data visualization is a technique in data science field, allowing
you to tell a compelling story, visualizing data and findings in an
approachable and stimulating way. It makes complex data look
simple and easy to understand.
10
matplotlib
Matplotlib has a important module called pyplot, which aids
in plotting figures. We have to import [Link] as
plt for making it call the package module.
11
matplotlib Key
points
oYou can Import required libraries and dataset to plot using
pandas pd.read_csv()
oUse [Link]()for plotting.
oUse [Link] , [Link] for labeling x and y-axis.
oUse [Link]() for setting the title of the plot.
oUse [Link]() for displaying the plot.
12
matplotlib example
import [Link] as plt
[Link]([1,2,3,4,5],[8,4,6,2,10], color='r')
[Link]('Number')
[Link]('Height')
[Link]('Wow! We Got Our First Bar Graph')
[Link]()
To create scatter or bar plots
[Link]()
[Link]()
13
Scatterplot
Read data from Automobile_data.csv and create a scatterplot that
shows relationship between average-mileage and horsepower.
import [Link] as plt
import pandas as pd
df = pd.read_csv("E:\\Automobile_data.csv")
df1 = df["average-mileage"]
df2 = df["horsepower"]
[Link](df1, df2, color='blue')
[Link]('Mileage')
[Link](‘Horsepower')
[Link]('About as simple as it gets, folks')
[Link]()
14
matplotlib Lab
Exercise
Read data from Automobile_data.csv and create a scatterplot that
shows relationship between mileage and wheel-base.
Due on LMS: Friday 17th April before midnight (11:59 pm)
15