How to apply a function to two columns of Pandas dataframe
To apply a function to two columns of a Pandas DataFrame, you can use the apply()
method of the DataFrame and pass the function as an argument. The apply()
method applies the function to each row of the DataFrame, and you can specify which columns to pass to the function by using the axis
parameter.
Here's an example of how you can use the apply()
method to apply a function to two columns of a DataFrame:
import pandas as pd
# Load the data
df = pd.read_csv('data.csv')
# Define the function
def multiply(row):
return row['column_1'] * row['column_2']
# Apply the function to the DataFrame
df['result'] = df.apply(multiply, axis=1)
This will apply the multiply()
function to each row of the DataFrame, passing the values in the 'column_1' and 'column_2' columns as arguments. The result of the function will be stored in a new column called 'result'.
You can also use the apply()
method to apply a function that takes multiple arguments to a DataFrame. To do this, you can define the function with multiple arguments and pass the columns to the function as separate arguments.
For example:
import pandas as pd
# Load the data
df = pd.read_csv('data.csv')
# Define the function
def multiply(value_1, value_2):
return value_1 * value_2
# Apply the function to the DataFrame
df['result'] = df.apply(lambda row: multiply(row['column_1'], row['column_2']), axis=1)
This will apply the multiply()
function to each row of the DataFrame, passing the values in the 'column_1' and 'column_2' columns as separate arguments. The result of the function will be stored in a new column called 'result'.