-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ex_Sheet_4_Num_2.py
53 lines (39 loc) · 1.61 KB
/
Ex_Sheet_4_Num_2.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
"""
Author: Gabriele Pompa
Identification Number: ABCXYZ [substitute with your Unisi Identification Number]
Date: 30-Mar-2020
File name: Ex_Sheet_4_Num_2.py
Description: This script filters rows of a given DataFrame based on value and
smaller-or-equal condition applied to the first column.
Then, select second and last columns.
"""
import numpy as np
import pandas as pd
def main():
"""
Function main() filters rows of a given DataFrame based on value and
smaller-or-equal condition applied to the first column.
Then, select second and last columns.
Parameters:
None
Returns:
None
"""
# test DataFrame
rows = 10
cols = 5
mat_decimal = np.array([[i**k for i in np.linspace(0,2,rows)] for k in range(1,cols+1)]).T
x_axis = [i for i in np.linspace(0,2,10)]
df_decimal = pd.DataFrame(data = mat_decimal,
index = x_axis,
columns=['x', 'x^2', 'x^3', 'x^4', 'x^5'])
value = 1.5
# calculations (all calculations are one-line. No need for dedicated functions)
df_decimal_rows_filtered = df_decimal[df_decimal['x'] <= value]
df_decimal_rows_and_cols_filtered = df_decimal_rows_filtered.iloc[:, [1,-1]]
# Print section ('\n' is to print on the next line)
print("df_decimal \n{}".format(df_decimal))
print("df_decimal_rows_filtered \n{}".format(df_decimal_rows_filtered))
print("df_decimal_rows_and_cols_filtered \n{}".format(df_decimal_rows_and_cols_filtered))
if __name__ == "__main__":
main()