Adding suffix to variable for every iteration of for loop in python

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 and T) 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?

>Solution :

Maybe with enumerate and globals ?

for idx, df in enumerate((exam_1, exam_2), start=1):
    # Remove subjects with score less than 60
    df = df[df['Score'] >= 60]

    # Dataframe of names starting with 'J' or 'T'
    globals()[f'J_{idx}'] = df[df['Name'].str.startswith('J')]
    globals()[f'T_{idx}'] = df[df['Name'].str.startswith('T')]

NB : This will create the variables (T_1, J_1, T_2 and J_2) as a pandas.core.frame.DataFrame in the global scope.

Leave a Reply