Home > other >  how to use lambda inside a function
how to use lambda inside a function

Time:03-17

I am trying to create a function to perform operations with columns of a data frame, but in the end it gives me an error because when defining the lambda x: x.variable, variable takes it literally, how do I assign the value it has? variable in the x.

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

def example(dataFrame, variable):
  dataFrame= dataFrame.assign(newColumn= lambda x: x.variable**2)
example(df,'col1')

AttributeError: 'DataFrame' object has no attribute 'variable'

How can i fix this?

CodePudding user response:

Changed the x.variable to x[variable] inside your lambda

def example(dataFrame, variable):
    dataFrame = dataFrame.assign(newColumn = lambda x: x[variable]**2)
    return dataFrame # you probably want to return the dataFrame as well

example(df,'col1')

df.column_name (x.variable) can be used only if the column already exists, in your case you are creating a new column and the property you are calling does not exist on that dataframe yet. So you can use df['new column name'] (x[variable])

  • Related