0

I have a python function defined:

def func_1(df):
    df_1 = df.drop_duplicates('Heir_1')
    value = df['Amount_1'].sum()
    return df_1

This is a value basic and simplified representation of my function. I just need to return two values from a function, one dataframe and other a variable. Is there a way i can do it?

I tried using a variable inside a function which i can use outside: the function: func_1.newone = df_1['Heir_2'].tolist()

I can access this outside the function but this variable does not update at each instance the function is called so it gives the same value again and again.

I also tried to save it in the dataframe like below but this gives me an error.

df_1['var1'] = df_1['Heir_2'].tolist()

Error:
ValueError: Length of values does not match length of index

Is there a solution to this? My main aim is just to reuse the value of a variable outside a function either by putting it into a return dataframe which i can access at the end of each iteration.

RSM
  • 645
  • 10
  • 25
  • 1
    Does this answer your question? [How can I return two values from a function in Python?](https://stackoverflow.com/questions/9752958/how-can-i-return-two-values-from-a-function-in-python) – PhilippNagel Oct 14 '20 at 18:49
  • Thanks @PhilippNagel That seems almost the thing that was looking for. Let me validate that answer in my code – RSM Oct 14 '20 at 18:55
  • It works properly. Closing question. – RSM Oct 14 '20 at 19:00

0 Answers0