0% found this document useful (1 vote)
161 views2 pages

676 Rows × 17 Columns: Import As

This document shows code for importing a CSV file containing weather data into a Pandas dataframe. It then explores the data by viewing the first few rows, checking data types of columns, and dropping unwanted columns and rows. The code loads weather data from Jaipur, India into a dataframe, examines the structure and content, and cleans the data by removing unneeded fields and rows.

Uploaded by

Abhigyan Kesri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
161 views2 pages

676 Rows × 17 Columns: Import As

This document shows code for importing a CSV file containing weather data into a Pandas dataframe. It then explores the data by viewing the first few rows, checking data types of columns, and dropping unwanted columns and rows. The code loads weather data from Jaipur, India into a dataframe, examines the structure and content, and cleans the data by removing unneeded fields and rows.

Uploaded by

Abhigyan Kesri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In [1]: #import pandas

import pandas as pd

In [2]: #Reading the csv file


df = pd.read_csv("JaipurFinalCleanData.csv")
type(df)

pandas.core.frame.DataFrame
Out[2]:

In [3]: # Exploring the data


df

Out[3]: date mean_temperature max_temperature min_temperature Mean_dew_pt mean_pressure max_humidity min_humidity max_dew_pt_1

2016-
0 34 41 27 6 1006.00 27 5 12
05-04

2016-
1 31 38 24 7 1005.65 29 6 13
05-05

2016-
2 28 34 21 11 1007.94 61 13 16
05-06

2016-
3 30 38 23 13 1008.39 69 18 17
05-07

2016-
4 34 41 26 10 1007.62 50 8 14
05-08

... ... ... ... ... ... ... ... ... ...

2018-
671 24 32 15 4 1015.39 48 6 9
03-07

2018-
672 24 32 15 2 1014.07 55 5 8
03-08

2018-
673 26 33 19 1 1014.41 42 7 5
03-09

2018-
674 26 34 19 3 1014.16 37 8 6
03-10

2018-
675 26 34 18 4 1013.76 38 6 8
03-11

676 rows × 17 columns

In [4]: #To access first 5 rows of data from the Jaipur csv file
print(df.head())

date mean_temperature max_temperature min_temperature \


0 2016-05-04 34 41 27
1 2016-05-05 31 38 24
2 2016-05-06 28 34 21
3 2016-05-07 30 38 23
4 2016-05-08 34 41 26

Mean_dew_pt mean_pressure max_humidity min_humidity max_dew_pt_1 \


0 6 1006.00 27 5 12
1 7 1005.65 29 6 13
2 11 1007.94 61 13 16
3 13 1008.39 69 18 17
4 10 1007.62 50 8 14

max_dew_pt_2 min_dew_pt_1 min_dew_pt_2 max_pressure_1 max_pressure_2 \


0 10 -2 -2 1009 1008
1 12 0 -2 1008 1009
2 13 6 0 1011 1008
3 16 9 6 1011 1011
4 17 6 9 1010 1011

min_pressure_1 min_pressure_2 rainfall


0 1000 1001 0.0
1 1001 1000 0.0
2 1003 1001 5.0
3 1004 1003 0.0
4 1002 1004 0.0

In [5]: #To access first 5 rows of data from the Jaipur csv file
print(df.head())

date mean_temperature max_temperature min_temperature \


0 2016-05-04 34 41 27
1 2016-05-05 31 38 24
2 2016-05-06 28 34 21
3 2016-05-07 30 38 23
4 2016-05-08 34 41 26

Mean_dew_pt mean_pressure max_humidity min_humidity max_dew_pt_1 \


0 6 1006.00 27 5 12
1 7 1005.65 29 6 13
2 11 1007.94 61 13 16
3 13 1008.39 69 18 17
4 10 1007.62 50 8 14

max_dew_pt_2 min_dew_pt_1 min_dew_pt_2 max_pressure_1 max_pressure_2 \


0 10 -2 -2 1009 1008
1 12 0 -2 1008 1009
2 13 6 0 1011 1008
3 16 9 6 1011 1011
4 17 6 9 1010 1011

min_pressure_1 min_pressure_2 rainfall


0 1000 1001 0.0
1 1001 1000 0.0
2 1003 1001 5.0
3 1004 1003 0.0
4 1002 1004 0.0

In [6]: # To find out the type of data (i.e., string, float, integer)
df.dtypes

date object
Out[6]:
mean_temperature int64
max_temperature int64
min_temperature int64
Mean_dew_pt int64
mean_pressure float64
max_humidity int64
min_humidity int64
max_dew_pt_1 int64
max_dew_pt_2 int64
min_dew_pt_1 int64
min_dew_pt_2 int64
max_pressure_1 int64
max_pressure_2 int64
min_pressure_1 int64
min_pressure_2 int64
rainfall float64
dtype: object

In [7]: # To Drop a column


df = df.drop(["max_dew_pt_2"], axis=1)

In [ ]: # To Drop a row at index 1


df = df.drop(1, axis=0)

In [10]: df.dtypes

date object
Out[10]:
mean_temperature int64
max_temperature int64
min_temperature int64
Mean_dew_pt int64
mean_pressure float64
max_humidity int64
min_humidity int64
max_dew_pt_1 int64
min_dew_pt_1 int64
min_dew_pt_2 int64
max_pressure_1 int64
max_pressure_2 int64
min_pressure_1 int64
min_pressure_2 int64
rainfall float64
dtype: object

In [12]: print(df.head())

date mean_temperature max_temperature min_temperature \


0 2016-05-04 34 41 27
2 2016-05-06 28 34 21
3 2016-05-07 30 38 23
4 2016-05-08 34 41 26
5 2016-05-09 34 42 27

Mean_dew_pt mean_pressure max_humidity min_humidity max_dew_pt_1 \


0 6 1006.00 27 5 12
2 11 1007.94 61 13 16
3 13 1008.39 69 18 17
4 10 1007.62 50 8 14
5 8 1006.73 32 7 12

min_dew_pt_1 min_dew_pt_2 max_pressure_1 max_pressure_2 min_pressure_1 \


0 -2 -2 1009 1008 1000
2 6 0 1011 1008 1003
3 9 6 1011 1011 1004
4 6 9 1010 1011 1002
5 6 6 1010 1010 1002

min_pressure_2 rainfall
0 1001 0.0
2 1001 5.0
3 1003 0.0
4 1004 0.0
5 1002 0.0

In [14]: # To Sort the values in descending order of date and print the first 5 rows
jaipur_weather = df.sort_values(by='date',ascending = False)
print(jaipur_weather.head())

date mean_temperature max_temperature min_temperature \


675 2018-03-11 26 34 18
674 2018-03-10 26 34 19
673 2018-03-09 26 33 19
672 2018-03-08 24 32 15
671 2018-03-07 24 32 15

Mean_dew_pt mean_pressure max_humidity min_humidity max_dew_pt_1 \


675 4 1013.76 38 6 8
674 3 1014.16 37 8 6
673 1 1014.41 42 7 5
672 2 1014.07 55 5 8
671 4 1015.39 48 6 9

min_dew_pt_1 min_dew_pt_2 max_pressure_1 max_pressure_2 \


675 0 -1 1017 1017
674 -1 -5 1017 1017
673 -5 -6 1017 1017
672 -6 -3 1017 1018
671 -3 0 1018 1017

min_pressure_1 min_pressure_2 rainfall


675 1009 1009 0.0
674 1009 1011 0.0
673 1011 1011 0.0
672 1011 1012 0.0
671 1012 1011 0.0

In [15]: # To Sort the values in ascending order of mean temperature and print the first 5 rows
jaipur_weather = df.sort_values(by='mean_temperature',ascending = True)
print(jaipur_weather.head())

date mean_temperature max_temperature min_temperature \


252 2017-01-11 10 18 3
253 2017-01-12 12 19 4
258 2017-01-17 12 20 5
255 2017-01-14 12 20 5
254 2017-01-13 12 20 4

Mean_dew_pt mean_pressure max_humidity min_humidity max_dew_pt_1 \


252 3 1017.00 94 17 9
253 -3 1017.54 70 13 2
258 3 1017.35 74 15 7
255 -1 1017.75 70 10 1
254 -5 1017.24 75 4 2

min_dew_pt_1 min_dew_pt_2 max_pressure_1 max_pressure_2 \


252 -5 -1 1019 1018
253 -7 -5 1020 1019
258 -2 0 1019 1021
255 -8 -93 1020 1020
254 -93 -7 1020 1020

min_pressure_1 min_pressure_2 rainfall


252 1015 1014 0.0
253 1015 1015 0.0
258 1015 1015 0.0
255 1016 1015 0.0
254 1015 1015 0.0

In [16]: # Using matplotlib to start plotting some graphs


import matplotlib.pyplot as plt
import numpy as np

In [17]: # Scatter Plot


x = df.date
y = df.mean_temperature
plt.scatter(x,y)
plt.xticks(np.arange(0, 676, 60))
plt.xticks (rotation=90)

(array([ 0, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 660]),
Out[17]:
[Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, '')])

In [22]: # Add x and y labels and set a font size


plt.scatter(x,y)
plt.xticks(np.arange(0,676,60),rotation=90)
plt.xlabel ("Date", fontsize = 14)
plt.ylabel ("Mean Temperature", fontsize = 14)
plt.title('Mean Temperature at Jaipur', fontsize = 20)
plt.show()

In [23]: # Line Plots


plt.figure(figsize=(20,10))
x = df.date
y_1 = df.max_temperature
y_2 = df.min_temperature
y_3 = df.mean_temperature
z = y_1-y_2
plt.plot(x,y_1, label = "Max temp")
plt.plot(x,y_2, label = "Min temp")
plt.plot(x,y_3, label = "Mean temp")
plt.plot(x,z, label = "range")
plt.xticks(np.arange(0, 676, 60))
plt.xticks (rotation=30)
plt.legend()
plt.show()

In [2]: import cv2 # import opencv


from matplotlib import pyplot as plt # import matplotlib
import numpy as np # import numpy

In [3]: #Load the image file into memory


img = cv2.imread('Images/flower.jpg')
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('flower image')
plt.axis('on')
plt.show()
print(img.shape)

(5655, 3770, 3)

In [4]: #Display image as a grayscale image


img = cv2.imread('Images/flower.jpg',0) #the number zero opens the image as a grayscale image
plt.imshow(img,cmap = 'gray') #cmap specifies color mapping, gray in this case.
plt.title('flower image')
plt.axis('on')
plt.show()
print(img.shape)

(5655, 3770)

In [5]: # Cropping images


img = cv2.imread('Images/flower.jpg') #Load the image file into memory
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
roi = img[2000:3100,1400:2650] #img[range of y, range of x]
plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))
plt.title('flower')
plt.axis('off')
plt.show()

In [6]: # Copy 'flower' in multiple places


img = cv2.imread('Images/flower.jpg')
flower = img[2000:3100,1400:2650]
img[0:1100,0:1250]=img[0:1100,2500:3750]=img[4555:5800,0:1250]=img[4555:5800,2500:3750]=flower
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('more flowers')
plt.axis('on')
plt.show()

In [7]: # access the RGB pixel located at x=500, y=500


[B,G,R] = img[500, 500] #img[y,x]
print('Red=', R, 'Green=', G, 'Blue=',B)

Red= 180 Green= 115 Blue= 35

In [8]: #saving our images using the imwrite function


cv2.imwrite('more_flower.jpg',img)

True
Out[8]:

In [9]: # Resizing images, maintain aspect ratio


img = cv2.imread('Images/flower.jpg')
print(img.shape)
resized=cv2.resize(img,(int(img.shape[1]/4),int(img.shape[0]/4)))
plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
plt.title('Flower')
plt.axis('off')
plt.show()
print(resized.shape)

(5655, 3770, 3)

(1413, 942, 3)

You might also like