-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ex_Sheet_4_Num_1.py
52 lines (38 loc) · 1.55 KB
/
Ex_Sheet_4_Num_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
"""
Author: Gabriele Pompa
Identification Number: ABCXYZ [substitute with your Unisi Identification Number]
Date: 30-Mar-2020
File name: Ex_Sheet_4_Num_1.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
magic_matrix = np.array([[7, 12, 1, 14], [2, 13, 8, 11], [16, 3, 10 , 5], [9, 6, 15, 4]])
df_magic = pd.DataFrame(data=magic_matrix,
index=['Row_' + str(i) for i in range(1,5)],
columns=['Col_' + str(i) for i in range(1,5)])
col_wise_sum = df_magic.sum()
row_wise_sum = df_magic.sum(axis=1)
main_diag_sum = np.diag(df_magic).sum()
other_diag_sum = np.diag(np.fliplr(df_magic)).sum()
# Print section ('\n' is to print on the next line)
print("df_magic \n{}".format(df_magic))
print("Sum of each row \n{}".format(col_wise_sum))
print("Sum of each column \n{}".format(row_wise_sum))
print("Sum of the main diagonal \n{}".format(main_diag_sum))
print("Sum of the other diagonal \n{}".format(other_diag_sum))
if __name__ == "__main__":
main()