Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
112 views

Python Pandas Matplot

The document discusses user-defined functions in Python and provides examples using the pandas and matplotlib packages. It introduces user-defined functions and how to create them. It then covers how to use pandas to load CSV data, display rows of a dataframe, sort values, and find maximum values. Finally, it discusses matplotlib for data visualization, how to import it, key plotting functions, and provides an example scatter plot using pandas to load data from a CSV file. Exercises are provided to create additional plots between variables in the automotive data.

Uploaded by

Rafay Farooq
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Python Pandas Matplot

The document discusses user-defined functions in Python and provides examples using the pandas and matplotlib packages. It introduces user-defined functions and how to create them. It then covers how to use pandas to load CSV data, display rows of a dataframe, sort values, and find maximum values. Finally, it discusses matplotlib for data visualization, how to import it, key plotting functions, and provides an example scatter plot using pandas to load data from a CSV file. Exercises are provided to create additional plots between variables in the automotive data.

Uploaded by

Rafay Farooq
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

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(df.head(5))
print(df.tail(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']][df.price==df['price'].max()]

Find the car with highest horsepower?

 7
pandas
Find cars with horsepower more than 200

new_df = df [['company',horsepower']][df.horsepower > 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 matplotlib.pyplot 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 plt.plot()for plotting.

oUse plt.xlabel , plt.ylabel for labeling x and y-axis.

oUse plt.title() for setting the title of the plot.

oUse plot.show() for displaying the plot.

 12
matplotlib example
import matplotlib.pyplot as plt

plt.plot([1,2,3,4,5],[8,4,6,2,10], color='r')
plt.xlabel('Number')
plt.ylabel('Height')
plt.title('Wow! We Got Our First Bar Graph')
plt.show()

To create scatter or bar plots

plt.scatter()
plt.bar()

 13
Scatterplot
Read data from Automobile_data.csv and create a scatterplot that
shows relationship between average-mileage and horsepower.

import matplotlib.pyplot as plt


import pandas as pd

df = pd.read_csv("E:\\Automobile_data.csv")

df1 = df["average-mileage"]
df2 = df["horsepower"]

plt.scatter(df1, df2, color='blue')

plt.xlabel('Mileage')
plt.ylabel(‘Horsepower')
plt.title('About as simple as it gets, folks')
plt.show()
 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

You might also like