I need to apply different functions for a database python depending on a slice of this dataframe. Each function creates several columns.
Here is my attempt (very simplified version of course) :
df=pd.DataFrame({'Type':['O','O','A'],'A':[7,9,8],'B':[8,6,5]})
def f1(df):
df['test']='A-OK'
df['test2']='A-OK2'
return df
def f2(df):
df['test']='O-OK'
df['test2']='O-OK2'
return df
def function_test(df):
df_a=df[df.Type =='A']
df_o=df[df.Type =='O']
#Applying functions
df_a=f1(df_a)
df_o=f2(df_o)
#retrieving results
df[df.Type =='A']=df_a
df[df.Type =='O']=df_o
return df
function_test(df)
The dataframes df_o and df_a (temporary) contains the good information, but I cannot copy the results in the original df after that.
Edit : Typos corrected