-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpd_dict_comp.py
More file actions
33 lines (27 loc) · 937 Bytes
/
pd_dict_comp.py
File metadata and controls
33 lines (27 loc) · 937 Bytes
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
import pandas
# 25 May 2022
# LOOPING THROUGH DICTIONARIES FOR LOOP
student_dict = {
"student": ["Albedo", "Ningguang", "Noelle", "Zhongli"],
"score": [99, 98, 90, 96]
}
# Retrieve keys from each item in dict
for (key, value) in student_dict.items():
print(key)
# Retrieve values from each key in dict
for (key, value) in student_dict.items():
print(value)
# LOOPING W PANDAS DF & HOW TO ITERATE OVER PANDAS DF
student_df = pandas.DataFrame(student_dict)
print(student_df)
# TODO 1: Loop through a DF.
for (key, value) in student_df.items():
print(value)
# TODO 2: Loop through rows of a DF, rather than columns using method "iterrows".
# index == the number in the first column of the df
for (index, row) in student_df.iterrows():
# print(index)
# print(row)
# print(row.student)
if row.student == "Ningguang":
print(f"Ningguang's score is: {row.score}")