I have a dataframe with values like
A B 1 4 2 6 3 9
I need to add a new column by adding values from column A and B, like
A B C 1 4 5 2 6 8 3 9 12
I believe this can be done using lambda function, but I can't figure out how to do it.
To create a new column, use the [] brackets with the new column name at the left side of the assignment.
Create New Columns in Pandas DataFrame Based on the Values of Other Columns Using the DataFrame. apply() Method. It applies the lambda function defined in the apply() method to each row of the DataFrame items_df and finally assigns the series of results to the Final Price column of the DataFrame items_df .
Very simple:
df['C'] = df['A'] + df['B']
Building a little more on Anton's answer, you can add all the columns like this:
df['sum'] = df[list(df.columns)].sum(axis=1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With