Apply same set of code to different subsets

Hello, I want to perform the same data preprocessing code with pandas to different subsets of dataframe. Is there any way to write the code more efficiently without repeating same code for 6 subsets?

Thank you!

Put the code in a function and then call it for each subset?

1 Like