I am trying to apply a function, that calculates a max value, over a list of ids and save them in one file using another function. Is this right way to do it? Because I am getting redundant results.
data1
animals_age1 = pd.DataFrame({'Animal': ['Falcon', 'Falcon','Falcon', 'Falcon', 'Falcon'],
'Age': [10, 20, 30, 40, 50]})
function1 (calculates max)
def function_1(df):
df = df[df.Age >=0]
return df.groupby(['Animal'])\
.apply(lambda x:pd.Series({'Age_max':x.Age.max()})).reset_index()
data2
animals_age2 = pd.DataFrame({'Animal': ['Falcon', 'Falcon','Falcon', 'Falcon', 'Falcon',
'Parrot', 'Parrot','Parrot', 'Parrot', 'Parrot'],
'Age': [10, 20, 30, 40, 50, 10, 20, 30, 40, 60]})
function2 (calculates max for a list of unique ids)
def function_2(df):
results = []
for id in df['Animal'].unique():
results.append(function_1(df))
results = pd.concat(results, axis=0)
return results