I have searched for similarly worded questions but haven't found one that answers my question.
I have 2 dataframes showing the results on exam_1
and exam_2
:
exam_1 = pd.DataFrame([['Jim', 87], ['Toby', 44], ['Joe', 71], ['Tom', 59]], columns=['Name', 'Score'])
exam_2 = pd.DataFrame([['Tom', 88], ['Joe', 55], ['Jim', 62], ['Toby', 70]], columns=['Name', 'Score'])
I want to iterate through these dataframes such that we:
- remove subjects with
Score
less than 60 - create new dataframes (
J
andT
) based on the first letter of the subjects name:
for df in (exam_1, exam_2):
# Remove subjects with score less than 60
df = df[df['Score'] >= 60]
# Dataframe of names starting with 'J' or 'T'
J = df[df['Name'].str.startswith('J')]
T = df[df['Name'].str.startswith('T')]
I want to add a suffix to the dataframes J
and T
based off the iteration in the for loop
For example, exam_1
is the 1st iteration, so the dataframes would be J_1
and T_1
.
exam_2
is the 2nd iteration, so the dataframes would be J_2
and T_2
.
Is this possible to do?