Python Pandas Matplot
Python Pandas Matplot
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
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)
6
pandas
Find the most expensive car company name
new_df = df [['company','price']][df.price==df['price'].max()]
7
pandas
Find cars with horsepower more than 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
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()
plt.scatter()
plt.bar()
13
Scatterplot
Read data from Automobile_data.csv and create a scatterplot that
shows relationship between average-mileage and horsepower.
df = pd.read_csv("E:\\Automobile_data.csv")
df1 = df["average-mileage"]
df2 = df["horsepower"]
plt.xlabel('Mileage')
plt.ylabel(‘Horsepower')
plt.title('About as simple as it gets, folks')
plt.show()
14
matplotlib Lab
Exercise
15