Tuesday, 30 June 2020

Python : Class


Class in Python

# Class name: Employee
# 2 Methods/Function: set_name, set_salary
# 2 Attributes: name, salary
# 1 object/instance: emp

class Employee:
    def set_name(self, new_name):
        self.name = new_name

    def set_salary(self, new_salary):
        self.salary = new_salary

  # Add a give_raise() method with raise amount as a parameter
    def give_raise(self, amount):
        self.salary = self.salary + amount
 
emp = Employee()
emp.set_name('Korel Rossi')
emp.set_salary(50000)

# Print the salary attribute of emp
print(emp.salary)

# Increase salary of emp by 1500
emp.salary = emp.salary + 1500

# Print the salary attribute of emp again
print(emp.salary)


class Employee:
            def set_name(self, new_name):
                self.name = new_name
       
            def set_salary(self, new_salary):
                self.salary = new_salary
       
            # Add a give_raise() method with raise amount as a parameter
            def give_raise(self, amount):
                self.salary = self.salary + amount
       
        emp = Employee()
        emp.set_name('Korel Rossi')
        emp.set_salary(50000)
       
        print(emp.salary)
        emp.give_raise(1500)
        print(emp.salary)

o/p
50000
51500

class Employee:
            def set_name(self, new_name):
                self.name = new_name
       
            def set_salary(self, new_salary):
                self.salary = new_salary
       
            def give_raise(self, amount):
                self.salary = self.salary + amount
       
            # Add monthly_salary method that returns 1/12th of salary attribute
            def monthly_salary(self):
                return self.salary / 12
           
        emp = Employee()
        emp.set_name('Korel Rossi')
        emp.set_salary(50000)
       
        # Get monthly salary of emp and assign to mon_sal
        mon_sal = emp.monthly_salary()
       
        # Print mon_sal
        print(mon_sal)
4166.666666666667


class Employee:
            # Create __init__() method
            def __init__(self, _name, _salary=0.0):
                # Create the name and salary attributes
                self.name = _name
                self.salary = _salary
           
            # From the previous lesson
            def give_raise(self, amount):
                self.salary += amount
       
            def monthly_salary(self):
                return self.salary/12
               
        emp = Employee("Korel Rossi")
        print(emp.name)
        print(emp.salary)
Korel Rossi
0.0


# Import datetime from datetime
         from datetime import datetime
        
         class Employee:
            
             def __init__(self, name, salary=0):
                 self.name = name
                 if salary > 0:
                   self.salary = salary
                 else:
                   self.salary = 0
                   print("Invalid salary!")
                  
                 # Add the hire_date attribute and set it to today's date
                 self.hire_date = datetime.today()
                   
         emp = Employee("Korel Rossi")
         print(emp.name)
         print(emp.hire_date)

Invalid salary!
Korel Rossi
2020-06-25 18:48:18.924258


        import numpy as np
        import math
       
        class Point:
            def __init__(self,x=0,y=0):
                self.x = x
                self.y = y
            def distance_to_origin(self):
                return math.sqrt(self.x**2 + self.y**2)
            def reflect(self,axis):
                self.axis = axis
                if axis=='x':
                    self.y = - self.y
                elif axis == 'y':
                    self.x = - self.x
                else:
                    print('Error')

Python : Pandas - 5



#Q: Find out the cars with more than 3 gears
#filter records with cars having more than 3 gears
gears = cars['gear'] > 3
# Apply the new filter to dataframe
carwithmoregears = cars[gears]
# view the new list with cylinder ascending order
carwithmoregears.sort_values(by='cyl')
Out[38]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
18
Honda Civic
30.4
4
75.7
52
4.93
1.615
18.52
1
1
4
6
27
Lotus Europa
30.4
4
95.1
113
3.77
1.513
16.90
1
1
5
6
26
Porsche 914-2
26.0
4
120.3
91
4.43
2.140
16.70
0
1
5
6
25
Fiat X1-9
27.3
4
79.0
66
4.08
1.935
18.90
1
1
4
3
19
Toyota Corolla
33.9
4
71.1
65
4.22
1.835
19.90
1
1
4
3
17
Fiat 128
32.4
4
78.7
66
4.08
2.200
19.47
1
1
4
3
31
Volvo 142E
21.4
4
121.0
109
4.11
2.780
18.60
1
1
4
6
8
Merc 230
22.8
4
140.8
95
3.92
3.150
22.90
1
1
4
6
7
Merc 240D
24.4
4
146.7
62
3.69
3.190
20.00
1
1
4
6
2
Datsun 710
22.8
4
108.0
93
3.85
2.320
18.61
1
1
4
3
9
Merc 280
19.2
6
167.6
123
3.92
3.440
18.30
1
1
4
12
1
Mazda RX4 Wag
21.0
6
160.0
110
3.90
2.875
17.02
0
1
4
12
29
Ferrari Dino
19.7
6
145.0
175
3.62
2.770
15.50
0
1
5
18
10
Merc 280C
17.8
6
167.6
123
3.92
3.440
18.90
1
1
4
12
0
Mazda RX4
21.0
6
160.0
110
3.90
2.620
16.46
0
1
4
12
30
Maserati Bora
15.0
8
301.0
335
3.54
3.570
14.60
0
1
5
24
28
Ford Pantera L
15.8
8
351.0
264
4.22
3.170
14.50
0
1
5
12
. . .
 #Q: Find out the cars with more than 3 gears and hp more than 100
#filter records with cars having more than 3 gears
f2 = (cars['gear'] > 3) & (cars['hp'] > 100)
New_list = cars[f2]
New_list

Out[39]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
0
Mazda RX4
21.0
6
160.0
110
3.90
2.620
16.46
0
1
4
12
1
Mazda RX4 Wag
21.0
6
160.0
110
3.90
2.875
17.02
0
1
4
12
9
Merc 280
19.2
6
167.6
123
3.92
3.440
18.30
1
1
4
12
10
Merc 280C
17.8
6
167.6
123
3.92
3.440
18.90
1
1
4
12
27
Lotus Europa
30.4
4
95.1
113
3.77
1.513
16.90
1
1
5
6
28
Ford Pantera L
15.8
8
351.0
264
4.22
3.170
14.50
0
1
5
12
29
Ferrari Dino
19.7
6
145.0
175
3.62
2.770
15.50
0
1
5
18
30
Maserati Bora
15.0
8
301.0
335
3.54
3.570
14.60
0
1
5
24
31
Volvo 142E
21.4
4
121.0
109
4.11
2.780
18.60
1
1
4
6
. . .

f3 = (cars['gear'] > 3) & (cars['hp'] > 100) & (cars['wt'] > 3)
New_list1 = cars[f3]
New_list1
Out[40]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
9
Merc 280
19.2
6
167.6
123
3.92
3.44
18.3
1
1
4
12
10
Merc 280C
17.8
6
167.6
123
3.92
3.44
18.9
1
1
4
12
28
Ford Pantera L
15.8
8
351.0
264
4.22
3.17
14.5
0
1
5
12
30
Maserati Bora
15.0
8
301.0
335
3.54
3.57
14.6
0
1
5
24
. . .

# Slicing using label -- loc
cars.loc[:]  # same result as cars.iloc[:]
Out[43]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
0
Mazda RX4
21.0
6
160.0
110
3.90
2.620
16.46
0
1
4
12
1
Mazda RX4 Wag
21.0
6
160.0
110
3.90
2.875
17.02
0
1
4
12
2
Datsun 710
22.8
4
108.0
93
3.85
2.320
18.61
1
1
4
3
3
Hornet 4 Drive
21.4
6
258.0
110
3.08
3.215
19.44
1
1
3
3
4
Hornet Sportabout
18.7
8
360.0
175
3.15
3.440
17.02
0
1
3
6
5
Valiant
18.1
6
225.0
105
2.76
3.460
20.22
1
1
3
3
6
Duster 360
14.3
8
360.0
245
3.21
3.570
15.84
0
1
3
12
7
Merc 240D
24.4
4
146.7
62
3.69
3.190
20.00
1
1
4
6
8
Merc 230
22.8
4
140.8
95
3.92
3.150
22.90
1
1
4
6
9
Merc 280
19.2
6
167.6
123
3.92
3.440
18.30
1
1
4
12
10
Merc 280C
17.8
6
167.6
123
3.92
3.440
18.90
1
1
4
12
11
Merc 450SE
16.4
8
275.8
180
3.07
4.070
17.40
0
1
3
9
12
Merc 450SL
17.3
8
275.8
180
3.07
3.730
17.60
0
1
3
9
13
Merc 450SLC
15.2
8
275.8
180
3.07
3.780
18.00
0
1
3
9
14
Cadillac Fleetwood
10.4
8
472.0
205
2.93
5.250
17.98
0
1
3
12
15
Lincoln Continental
10.4
8
460.0
215
3.00
5.424
17.82
0
1
3
12
16
Chrysler Imperial
14.7
8
440.0
230
3.23
5.345
17.42
0
1
3
12
17
Fiat 128
32.4
4
78.7
66
4.08
2.200
19.47
1
1
4
3
18
Honda Civic
30.4
4
75.7
52
4.93
1.615
18.52
1
1
4
6
19
Toyota Corolla
33.9
4
71.1
65
4.22
1.835
19.90
1
1
4
3
20
Toyota Corona
21.5
4
120.1
97
3.70
2.465
20.01
1
1
3
3
21
Dodge Challenger
15.5
8
318.0
150
2.76
3.520
16.87
0
1
3
6
22
AMC Javelin
15.2
8
304.0
150
3.15
3.435
17.30
0
1
3
6
23
Camaro Z28
13.3
8
350.0
245
3.73
3.840
15.41
0
1
3
12
24
Pontiac Firebird
19.2
8
400.0
175
3.08
3.845
17.05
0
1
3
6
25
Fiat X1-9
27.3
4
79.0
66
4.08
1.935
18.90
1
1
4
3
26
Porsche 914-2
26.0
4
120.3
91
4.43
2.140
16.70
0
1
5
6
27
Lotus Europa
30.4
4
95.1
113
3.77
1.513
16.90
1
1
5
6
28
Ford Pantera L
15.8
8
351.0
264
4.22
3.170
14.50
0
1
5
12
29
Ferrari Dino
19.7
6
145.0
175
3.62
2.770
15.50
0
1
5
18
30
Maserati Bora
15.0
8
301.0
335
3.54
3.570
14.60
0
1
5
24
31
Volvo 142E
21.4
4
121.0
109
4.11
2.780
18.60
1
1
4
6
. . .

cars.loc[4:7]   # results Row Index no 4, 5, 6 & 7
# BUT cars.iloc[4:7] results Row Index no 4, 5 & 6   It excludes the upper bound value indexed row
Out[50]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
4
Hornet Sportabout
18.7
8
360.0
175
3.15
3.44
17.02
0
1
3
6
5
Valiant
18.1
6
225.0
105
2.76
3.46
20.22
1
1
3
3
6
Duster 360
14.3
8
360.0
245
3.21
3.57
15.84
0
1
3
12
7
Merc 240D
24.4
4
146.7
62
3.69
3.19
20.00
1
1
4
6
. . .

cars.loc[4:]   # results from Row Index no 4
Out[53]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
4
Hornet Sportabout
18.7
8
360.0
175
3.15
3.440
17.02
0
1
3
6
5
Valiant
18.1
6
225.0
105
2.76
3.460
20.22
1
1
3
3
6
Duster 360
14.3
8
360.0
245
3.21
3.570
15.84
0
1
3
12
7
Merc 240D
24.4
4
146.7
62
3.69
3.190
20.00
1
1
4
6
8
Merc 230
22.8
4
140.8
95
3.92
3.150
22.90
1
1
4
6
9
Merc 280
19.2
6
167.6
123
3.92
3.440
18.30
1
1
4
12
10
Merc 280C
17.8
6
167.6
123
3.92
3.440
18.90
1
1
4
12
11
Merc 450SE
16.4
8
275.8
180
3.07
4.070
17.40
0
1
3
9
12
Merc 450SL
17.3
8
275.8
180
3.07
3.730
17.60
0
1
3
9
13
Merc 450SLC
15.2
8
275.8
180
3.07
3.780
18.00
0
1
3
9
14
Cadillac Fleetwood
10.4
8
472.0
205
2.93
5.250
17.98
0
1
3
12
15
Lincoln Continental
10.4
8
460.0
215
3.00
5.424
17.82
0
1
3
12
16
Chrysler Imperial
14.7
8
440.0
230
3.23
5.345
17.42
0
1
3
12
17
Fiat 128
32.4
4
78.7
66
4.08
2.200
19.47
1
1
4
3
18
Honda Civic
30.4
4
75.7
52
4.93
1.615
18.52
1
1
4
6
19
Toyota Corolla
33.9
4
71.1
65
4.22
1.835
19.90
1
1
4
3
20
Toyota Corona
21.5
4
120.1
97
3.70
2.465
20.01
1
1
3
3
21
Dodge Challenger
15.5
8
318.0
150
2.76
3.520
16.87
0
1
3
6
22
AMC Javelin
15.2
8
304.0
150
3.15
3.435
17.30
0
1
3
6
23
Camaro Z28
13.3
8
350.0
245
3.73
3.840
15.41
0
1
3
12
24
Pontiac Firebird
19.2
8
400.0
175
3.08
3.845
17.05
0
1
3
6
25
Fiat X1-9
27.3
4
79.0
66
4.08
1.935
18.90
1
1
4
3
26
Porsche 914-2
26.0
4
120.3
91
4.43
2.140
16.70
0
1
5
6
27
Lotus Europa
30.4
4
95.1
113
3.77
1.513
16.90
1
1
5
6
28
Ford Pantera L
15.8
8
351.0
264
4.22
3.170
14.50
0
1
5
12
29
Ferrari Dino
19.7
6
145.0
175
3.62
2.770
15.50
0
1
5
18
30
Maserati Bora
15.0
8
301.0
335
3.54
3.570
14.60
0
1
5
24
31
Volvo 142E
21.4
4
121.0
109
4.11
2.780
18.60
1
1
4
6
. . .

cars.loc[:6]   # results from Row Index no 0 to Row Index no 6
# BUT cars.iloc[:6]   # results from Row Index no 0 to Row Index no 5
Out[58]:
Unnamed: 0
mpg
cyl
disp
hp
drat
wt
qsec
vs
am
gear
carb
0
Mazda RX4
21.0
6
160.0
110
3.90
2.620
16.46
0
1
4
12
1
Mazda RX4 Wag
21.0
6
160.0
110
3.90
2.875
17.02
0
1
4
12
2
Datsun 710
22.8
4
108.0
93
3.85
2.320
18.61
1
1
4
3
3
Hornet 4 Drive
21.4
6
258.0
110
3.08
3.215
19.44
1
1
3
3
4
Hornet Sportabout
18.7
8
360.0
175
3.15
3.440
17.02
0
1
3
6
5
Valiant
18.1
6
225.0
105
2.76
3.460
20.22
1
1
3
3
6
Duster 360
14.3
8
360.0
245
3.21
3.570
15.84
0
1
3
12
. . .

cars.loc[2:7,'hp':'am'] # Row from 2nd index to 7th index rows | Columns: from "hp" to "am" CONTINUOUSLY
Out[68]:
hp
drat
wt
qsec
vs
am
2
93
3.85
2.320
18.61
1
1
3
110
3.08
3.215
19.44
1
1
4
175
3.15
3.440
17.02
0
1
5
105
2.76
3.460
20.22
1
1
6
245
3.21
3.570
15.84
0
1
7
62
3.69
3.190
20.00
1
1
. . .

cars.loc[2:7,'hp':'am':2] # Row from 2nd index to 7th index rows | Columns: from "hp" to "am" with INTERVAL OF 2
Out[69]:
hp
wt
vs
2
93
2.320
1
3
110
3.215
1
4
175
3.440
0
5
105
3.460
1
6
245
3.570
0
7
62
3.190
1
. . .

cars.loc[2:7:2,'hp':'am':2]
# Row from 2nd index to 7th index rows with INTERVAL of 2 | Columns: from "hp" to "am" with INTERVAL OF 2
Out[70]:
hp
wt
vs
2
93
2.32
1
4
175
3.44
0
6
245
3.57
0