0

Using Pandas I have a data frame with rows and columns:

id  column1 column2 column3
1         4  Banana       2
2         4  Carrot       
3         1  Tomato       3
4         7   Melon       5
5         1    Lime       5

I want to iterate through each row and calculate the mean the items in column1 and column2 (e.g. Row 1: 4+2/2=3). Everything will be put in a new column called mean. empty values should be ignored.

The result should be like:

id  column1 column2 column3  mean
1         4  Banana       2     3
2         4  Carrot             4
3         1  Tomato       3     2
4         7   Melon       5     6
5         2    Lime       5   3.5

1 Answers1

1

You can use:

df['mean'] = df[['column1', 'column3']].mean(axis=1)
Carsten
  • 2,765
  • 1
  • 13
  • 28