0

all.

I have a function that returns two values. One is a list, the other is a double.

I want to use something like this to create two new columns in my df and use .apply to populate those columns on a row by row basis.

def f(a_list):
     #do some stuff to the list
     if(stuff):
          make_new_stuff_happen         

     #return results of stuff
     return new_list, a_double



def main():
     df['new_col1'], df['new_col2'] = df.apply(lambda x: f(x['some_col']))

Thanks for any help you can provide.

  • 2
    Does this answer your question? [how to create multiple columns at once with apply?](https://stackoverflow.com/questions/66267729/how-to-create-multiple-columns-at-once-with-apply) – dm2 Jun 22 '21 at 15:56
  • [Return multiple columns from pandas apply()](https://stackoverflow.com/q/23586510/15497888), [Pandas Apply Function That returns two new columns](https://stackoverflow.com/q/47969756/15497888) – Henry Ecker Jun 22 '21 at 16:27

1 Answers1

0

A few notes:

  • I think by double you mean a float in Python?
  • Even for examples, I'd name your function & vars something more meaningful, so it's easier to diagnose

Maybe this answer will help:

If this is the original dataframe you're working with:

col_1  |  col_2  |  col_3
-------------------------
  1    |    3    |   3
  2    |    3    |   4
  3    |    1    |   1

You can just have a function like this:

def transform_into_two_columns(original_val_from_row):
    
    # do some stuff to the list:
    
    # example 1: multiply each row by 2 & save output to new list (this would be "new_list" in your question)
    original_val_times_2 = original_val_from_row*2
    
    # example 2: sum all values in list/column (this would be "a_double" in your question)
    original_val_plus_2 = original_val_from_row+2.1

    return original_val_times_2, original_val_plus_2

Then, you can save that function's output to a list:

list_of_tuples = df['col_2'].apply(lambda x: transform_into_two_columns(x)).to_list()

Then, with that list_of_tuples, you can create 2 new columns:

df[['NEW_col_4', 'NEW_col_5']] = pd.DataFrame(list_of_tuples, index=df.index)

Your new dataframe will look like this:

col_1  |  col_2  |  col_3  | NEW_col_4  | NEW_col_5
---------------------------------------------------
  1    |    3    |   3     |     6      |    5.1   
  2    |    3    |   4     |     6      |    5.1
  3    |    1    |   1     |     2      |    3.1
Audrey
  • 51
  • 3