To apply a function to multiple columns in Pandas, you can use the apply
method. This method allows you to apply a function to a DataFrame or a specific column or set of columns.
For example, consider the following DataFrame:
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
This DataFrame has three columns A
, B
, and C
, with five rows of data.
To apply a function to all the columns in this DataFrame, you could do the following:
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
# Define the function to apply
def square(x):
return x ** 2
# Apply the function to the DataFrame
df_squared = df.apply(square)
# Print the resulting DataFrame
print(df_squared)
In the code above, the apply
method is applied to the DataFrame, and the square
function is passed as an argument.
This tells the apply
method to apply the square
function to each column in the DataFrame. The result is a new DataFrame
object containing the squared values for each column.
You can also use the apply
method to apply a function to a specific column or set of columns in a DataFrame. For example, if you wanted to apply the square
function to only column B
, you could do the following:
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
# Define the function to apply
def square(x):
return x ** 2
# Apply the function to column B
df_squared = df['B'].apply(square)
# Print the resulting Series
print(df_squared)
In the code above, the apply
method is applied to the B
column of the DataFrame, and the square
function is passed as an argument.
This tells the apply
method to apply the square
function to the B
column.
The result is a new Series
object containing the squared values for the B
column.
Related tutorials curated for you
How to convert a series to a list in Pandas
How to fix: AttributeError module 'pandas' has no attribute 'dataframe'
How to groupby mean in Pnadas
How to use Timedelta in Pandas
How to fix: ValueError: pandas cannot reindex from a duplicate axis
What is Pandas Cumsum()?
How to make a crosstab in Pandas
How to convert a series to a NumPy array in Pandas
How to round in Pandas
How to split a Pandas DataFrame by a column value
How to find the minimum in Pandas
How to use ewm() in Pandas