I have a dataframe with many columns. I am trying to filter one of those columns ('Region') and create a separate dataframe based on each of those 4 regions in the ''Region' column. And then run a large block of code that contains a bunch of calculations on each of those 4 separate dataframes without having to rewrite the large block of code 4 separate times.
I know i can use an .isin function for the column filtering and do this for my 4 regions (US, EM, Europe, Asia):
US = df[df['Region'].isin('US')]
EM = df[df['Region'].isin('EM')]
Europe = df[df['Region'].isin('Europe')]
Asia = df[df['Region'].isin('Asia')]
And then run my block of code on the 4 new dataframes. But i would be executing my large block of calculation code 4 separate times and it is just too messy. How can i do this in a loop so i only have to write my large block of code one time? If there is another function i can use to do this besides a for loop that would be awesome as well. Appreciate any help- trying to learn.
Dummy Code:
df = pd.DataFrame({'a':[1,2,3,4,5,6], 'b':['cats','dogs','birds','pianos','elephant','dinos'], 'Region' : ['EM', 'US', 'US', 'Europe', 'Asia', 'Asia']})