The method to perform a function on several columns in a data frame simultaneously is to use the apply() function with the axis parameter set to 1. This applies the function to each row of the data frame.
For example, to calculate the sum of two columns A and B of a data frame, you can write:
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['sum'] = df.apply(lambda row: row['A'] + row['B'], axis=1)
print(df)
Output:
A B sum
0 1 4 5
1 2 5 7
2 3 6 9
Here, the lambda function takes each row of the data frame and applies the sum function to the columns A and B, and stores the result in a new column 'sum'.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-10-26 11:00:00 +0000
Seen: 14 times
Last updated: May 12 '21
How can a data.frame with only one column be divided into several columns in R?
How can the columns be transformed into a multi-level structure?
How to arrange columns in a Flutter datatable?
What is the method to obtain a count from specific columns while disregarding the rest?
What are the steps to utilize a for loop for generating and populating columns?