How to apply a function to a column in a Pandas DataFrame?
Published on Aug. 22, 2023, 12:18 p.m.
To apply a function to a column in a Pandas DataFrame, you can use the apply()
method. Here’s an example:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # create a DataFrame with columns A and B
# define a function to add 1 to each value in the 'B' column
def add_one(x):
return x + 1
df['B'] = df['B'].apply(add_one) # apply the 'add_one' function to the 'B' column
print(df)
Output:
A B
0 1 5
1 2 6
2 3 7
In the example above, the add_one
function takes a single input and returns the input plus one. The apply()
method applies the add_one
function to each value in the ‘B’ column of the DataFrame and returns a new DataFrame with the modified values. The modified DataFrame is then stored back into the ‘B’ column of the original DataFrame.
You can also apply a function to multiple columns of a DataFrame by passing a list of column names to the apply()
method:
def multiply_by_two(x):
return x * 2
df[['A', 'B']] = df[['A', 'B']].apply(multiply_by_two) # apply the 'multiply_by_two' function to columns 'A' and 'B'
print(df)
Output:
A B
0 2 10
1 4 12
2 6 14
In this example, the multiply_by_two
function multiplies its input by two. The apply()
method applies the multiply_by_two
function to each value in columns ‘A’ and ‘B’ of the DataFrame and stores the modified values back into the same columns of the original DataFrame.
Overall, applying a function to a column or multiple columns of a Pandas DataFrame is easy using the apply()
method.