How to apply a function to severals pandas columns as arguments?

pandas apply function to multiple columns
pandas dataframe apply function with arguments
pandas apply function to column
apply a user defined function to a column in pandas
pandas apply lambda
pandas apply return multiple columns
pandas map multiple columns
pandas apply string function to column

Please can you help me to resolve my daily problem? I have a panda dataframe df with 2 columns called:

MatSituation  NbChild 
    1         0
    2         1
    3         0
    4         1

I want to create a new column with a function f which have "MatSituation" and "NbChild" arguments:

df[n_NbChild] = df['MatSituation ','NbChild '].apply(.....)

How can i manage this situation please?

Thanks a lot. Thierry

df['n_Age'] = df['Age'][dataset['Age'].notnull()].apply(age_classify)

You can just create a function that accepts two inputs and returns a value and then assign to it a new column.

For example:

def sum_two_columns(a, b):
    return a + b


df['New Column'] = sum_two_columns(df.MatSituation, df.NbChild)

df

MatSituation  NbChild   New Column
    1         0            1
    2         1            3
    3         0            3
    4         1            5

Applying function with multiple arguments to create a new pandas , I want to create a new column in a pandas data frame by applying a function to two existing columns. Following this fxy(x, y): return x * y. We will use Dataframe/series.apply() method to apply a function. Syntax: Dataframe/series.apply(func, convert_dtype=True, args=()) Parameters: This method will take following parameters : func: It takes a function and applies it to all values of pandas series. convert_dtype: Convert dtype as per the function’s operation. args=(): Additional

if age_classify is a function which accepts row as an argument - the only think you have to modify is : df['n_Age'] = df['Age'][dataset['Age'].notnull()].apply(age_classify, axis=1)

pandas.DataFrame.apply — pandas 0.20.2 documentation, Return type depends on whether passed function aggregates, or the reduce argument if the DataFrame is empty. Parameters: func : function. Function to apply to� Apply a lambda function to each column: To apply this lambda function to each column in dataframe, pass the lambda function as first and only argument in Dataframe.apply () with above created dataframe object i.e. # Apply a lambda function to each column by adding 10 to each value in each column

I modified my function:

def NbEnfNull(x,y):
    if (y.empty):
        if (x == 1 or x == 2): 
            return 0
        elif (x == 3 or x == 4): 
            return 1   
    else:
        return y

Call:

df['n_nbr_enf'] = NbEnfNull(df['COD_SIS_FAM'],df['NBR_ENF'])

But i have null value for df['n_nbr_enf']

df['n_nbr_enf'].isnull().sum()

1472 I don't know how to progress... Thanks a lot.

pandas.apply(): Apply a function to each row/column in Dataframe , args : tuple / list of arguments to passed to function. Let's use this to apply function to rows and columns of a Dataframe. Suppose we have a� Apply a lambda function to all the columns in dataframe using Dataframe.apply () and inside this lambda function check if column name is ‘z’ then square all the values in it i.e. # Apply function numpy.square () to square the value one column only i.e. with column name 'z' modDfObj = dfObj.apply(lambda x: np.square(x) if x.name == 'z' else x)

Pandas: Apply a function to single or selected columns or rows in , Pandas: Apply a function to single or selected columns or rows in Dataframe. Varun April Apply a function to a certain columns in Dataframe. Questions: I have some problems with the Pandas apply function, when using multiple columns with the following dataframe df = DataFrame ({'a' : np.random.randn(6), 'b' : ['foo', 'bar'] * 3, 'c' : np.random.randn(6)}) and the following function def my_test(a, b): return a % b When I try to apply this function with : df['Value'] =

Pandas DataFrame apply() Examples, Pandas DataFrame apply() function is used to apply a function along an axis of the The output will be different based on the value of the axis argument. pandas create new column based on values from other columns / apply a function of multiple columns, row-wise asked Oct 10, 2019 in Python by Sammy ( 47.8k points) pandas

pandas.DataFrame.apply — pandas 0.25.0.dev0+752.g49f33f0d , Otherwise, it depends on the result_type argument. Parameters: func : function. Function to apply to each column or row. 0 or ‘index’: apply function to each column. 1 or ‘columns’: apply function to each row. {0 or ‘index’, 1 or ‘columns’} Default Value: 0: Required: raw False : passes each row or column as a Series to the function. True : the passed function will receive ndarray objects instead. If you are just applying a NumPy reduction

Comments
  • what is your expected output?
  • Hello, Thanks. I tried your proposition but first parameter seems be row index: def NbEnfNull(x,y): print("Le code est {} et le nb d\'enfant est {}".format(x,y)) dataset['n_nbr_enf'] = NbEnfNull(dataset['COD_SIS_FAM'],dataset['NBR_ENF']) Output: Le code est 0 2 1 2 2 4 3 3 4 1 5 1 6 1 Then first parameter id a value between 1 and 4 so i don't understand...