Looping through dictionaries
student_dict = {
"student": ["Angela", "James", "Lily"],
"score": [56, 76, 98]
}
#Looping through dictionaries:
for (key, value) in student_dict.items():
print(key)
print(value)
-------
student
score
["Angela", "James", "Lily"]
[56, 76, 98]
Loop through rows of a data frame
student_dict = {
"student": ["Angela", "James", "Lily"],
"score": [56, 76, 98]
}
import pandas
student_data_frame = pandas.DataFrame(student_dict)
print(student_data_frame)
#Loop through rows of a data frame
for (index, value) in student_data_frame.items():
print(key)
print(value)
---------
#print(student_data_frame)
student score
0 Angela 56
1 James 76
2 Lily 98
#print(key)
student
score
#print(value)
student
0 Angela
1 James
2 Lily
Name: student, dtype: object
score
0 56
1 76
2 98
Name: score, dtype: int64
Iterrows() : Loop through rows of a data frame
student_dict = {
"student": ["Angela", "James", "Lily"],
"score": [56, 76, 98]
}
import pandas
student_data_frame = pandas.DataFrame(student_dict)
print(student_data_frame)
#Loop through rows of a data frame
for (index, row) in student_data_frame.iterrows():
print(index)
print(row)
print(row.student)
print(row.score)
if row.student == "Angela":
print(row.score)
---------
#print(index)
0
1
2
#print(row)
0
student Angela
score 56
Name: 0, dtype: object
1
student James
score 76
Name: 1, dtype: object
2
student Lily
score 98
Name: 2, dtype: object
#print(row.student)
Angela
James
Lily
#print(row.score)
56
76
98
- Each of the row is a panda series object ⇒ we can tap into the row and then get hold of the value under a particular column by using the dot notation.