-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
85 lines (59 loc) · 2.2 KB
/
code
File metadata and controls
85 lines (59 loc) · 2.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
data = {"name": ["Joseph", "Jacob", "Sam", "Jesee", "Ryan", "Lisa", "Lee"],
"job": ["teacher", "psychologist", "data scientist", "software developer", "psychologist", "psychologist", "teacher"],
"family_size": [3, 2, 1, 4, 2, 3, 2],
"num_cars": [3, 1, 1, 2, 2, 4, 1]}
import pandas as pd
data = {"name": ["Joseph", "Jacob", "Sam", "Jesee", "Ryan", "Lisa", "Lee"],
"job": ["teacher", "psychologist", "data scientist", "software developer", "psychologist", "psychologist", "teacher"],
"family_size": [3, 2, 1, 4, 2, 3, 2],
"num_cars": [2, 1, 1, 2, 2, 4, 1]}
#Q1-1
frame = pd.DataFrame(data, columns=["name", "job", "family_size", "num_cars"])
# print(frame)
#Q1-2
print(frame[(frame["job"].isin(["psychologist", "teacher"])) & (frame["num_cars"]> frame["family_size"])])
# print(frame[((frame.job == "psychologist") | (frame.job == "teacher")) & (frame.num_cars > frame.family_size)])
# #Q1-3
print(frame[(frame['family_size']<=2) &(frame['num_cars']>=1)])
#print(frame[(frame["family_size"]<=2)&(frame["num_cars"]>=1)])
# #Q1-4
print(len(set(frame["job"])))
# #Q2
import numpy as np
fruit = pd.Series(np.random.choice(['apple', 'banana', 'carrot'], 10))
weights = pd.Series(np.linspace(1, 10, 10))
weights.groupby(fruit).mean()
import numpy as np
course_name = np.array(['Python', 'JS', 'examples', 'PHP', 'html'])
# Q3-1
i = np.argsort(course_name)
print("Indices of the sorted elements of a given array:\n", i)
# Q3-2
print("Test if each element of the said array starts with 'P':")
r = np.char.startswith(course_name, 'P')
print(r)
import numpy as np
student_id = np.array([1023, 5202, 6230, 1671, 1682, 5241, 4532])
# Q4-1
reversed_arr = student_id[::-1]
print("Original array", student_id, "\nReversed array", str(reversed_arr))
# Q4-2
sorted_index_array = np.argsort(student_id)
sorted_arr = student_id[sorted_index_array]
# print(sorted_arr)
print("3 largest values:", sorted_arr[-3 : ] )
# #Q5
import numpy as np
x = np.arange(1, 100)
# find multiple of 3 or 5
n= x[(x % 3 == 0) | (x % 5 == 0)]
print(n)
print(n.sum())
# #Q6
import numpy as np
arr = np.arange(12).reshape(3,4)
print(arr)
arr= arr[:,[0,2,1,3]]
print(arr)
arr = arr [[1,0,2],:]
print(arr)