Search code examples
pandasfunctionrow

how to apply a function to each row of a pandas dataframe, where the input to the function is the elements in the row in the form of a list


I would like to apply a function to each row of a dataframe. The function takes in each row of the dataframe as a list. For example, I am computing a new column called 'membership' that takes the entire row of the data frame as input. But the input needs to be in the form of a list. The code below is not working.

df_sample['membership'] = df_sample.apply(lambda row: 
                     cluster_pred(df_sample.values.tolist()), axis = 1)

Solution

  • In your lambda, you've defined the incoming row as row, so you can just pass row.tolist():

    df_sample['membership'] = df_sample.apply(lambda row:
        cluster_pred(row.tolist()), axis=1)