Calculate new column as the mean of other columns in pandas [duplicate]

Use .mean. By specifying the axis you can take the average across the row or the column.

df['average'] = df.mean(axis=1)
df

returns

       salary_1  salary_2  salary_3     average
0       230       235       210  225.000000
1       345       375       385  368.333333
2       222       292       260  258.000000

If you only want the mean of a few you can select only those columns. E.g.

df['average_1_3'] = df[['salary_1', 'salary_3']].mean(axis=1)
df

returns

   salary_1  salary_2  salary_3  average_1_3
0       230       235       210        220.0
1       345       375       385        365.0
2       222       292       260        241.0

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)