Creating a new column in a Pandas dataframe by aggregating values from different columns

I have recently included a new column in the table called
and my intention now is to populate this column with values for each entry, based on the conditions of the
column values.
My dataframe consists of values like
My objective is to create a new column by combining values from column A and B, such as
I am under the impression that this can be achieved using a lambda function, but I am struggling to comprehend the process.


I have a dataframe with values like

1 4
2 6
3 9

It is necessary to create an additional column by combining the values in column A and B, referred to as
adding values

1 4 5
2 6 8
3 9 12

I think a lambda function can achieve this, but I’m unsure about the implementation.

Solution 1:

Very simple:

df['C'] = df['A'] + df['B']

Solution 2:

Expanding on Anton’s response, an alternative approach would be to include all the columns in the following manner:

df['sum'] = df[list(df.columns)].sum(axis=1)

Solution 3:

One option is to utilize the DeepSpace answer as the easiest approach. Nonetheless, if the preference is to use an anonymous function, the apply method can be employed.

df['C'] = df.apply(lambda row: row['A'] + row['B'], axis=1)

Solution 4:

As mentioned in the comment by @EdChum, you have the option of utilizing the


function to accomplish this.

df['C'] =  df[['A', 'B']].sum(axis=1)
In [245]: df
   A  B   C
0  1  4   5
1  2  6   8
2  3  9  12

Frequently Asked Questions