-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexe5.1.py
62 lines (40 loc) · 1.58 KB
/
exe5.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 20 14:03:08 2022
@author: jolien
"""
import numpy as np
import pandas as pd
import random
mtcars = pd.read_csv("/Users/jolien/Desktop/Python/mtcars.csv")
############################# Exercise 5.1 ####################################
#1.Sort mtcars by carNames, am and gear (in that order). Reset the index.
#Save it as mtcars2.
mtcars11 = mtcars.sort_values(["carNames", "am", "gear"])
mtcars11
mtcars2 = mtcars11.reset_index()
mtcars2.head()
mtcars2[2:3]
#2.Filter all out cars that an mpg bigger than 25
mtcars[mtcars["mpg"]>25]
#3.Filter all cars that have an mpg smaller or equal to 25 and hp value bigger
#than 75
mtcars[(mtcars["mpg"] <= 25) & (mtcars["hp"]>75)]
#4.Filter all cars that have 4 gears and an am value of 0
mtcars.columns
mtcars[(mtcars["gear"] == 4) & (mtcars["am"]==0)]
#5.Filter all cars that have 4 gears or an am value of 0
mtcars[(mtcars["gear"] == 4) | (mtcars["am"]==0)]
#6.Explain the difference between the results of ex. number 3 and 4 in your own
#words
#??? vielleicht meint er eher 4 und 5
# bei 4 müssen beide bedingungen erfüllt sein für true
#bei 5 muss nur die erste oder (!) die zweite bedingung erfüllt sein
#7.What do boolean values have to do with this line of code? Demonstrate
False and True # = False
True | False # = True
#8.Filter all cars that have a cyl value of 4 or greater and a vs value of 0
#or an am value of 1
mtcars[(mtcars["cyl"] >= 4) & (mtcars["vs"]==0) | (mtcars["am"] ==1)]
########################### Exercise 5.1: End #################################