Home > Blockchain >  Find row mean with selected columns using column index (Pandas)
Find row mean with selected columns using column index (Pandas)

Time:09-28

Let say I have a dataframe with 100 rows (names) and 30 columns.

I want to find the mean for the 100 names between columns index 5 to 15.

I was using the method of list out every single columns as stated below

test['avg_revenue21'] = test[['Jan 21 (Revenue)','Feb 21 (Revenue)','Mar 21 (Revenue)',
               'Apr 21 (Revenue)','May 21 (Revenue)','Jun 21 (Revenue)',
               'Jul 21 (Revenue)','Aug 21 (Revenue)','Sep 21 (Revenue)',
               'Oct 21 (Revenue)','Nov 21 (Revenue)','Dec 21 (Revenue)']].mean(axis=1)

I found this to be very tidous and troublesome.

Do we have a simpler way to do it?

CodePudding user response:

You can do simply:

test['avg_revenue21'] = test.iloc[:,5:15].mean()
  • Related