Here I am trying to pull average of rows 0 through 8 and columns "9/15/2020" through "9/18/2020" from df dataframe using .loc[] in pandas
[This is the df dataframe having 8 rows and 9 columns][1]
[1]: https://i.sstatic.net/QgjBp.png
Have tried using following long syntax : This works fine
Average = df.loc[[0,1,2,3,4,5,6,7,8] ,['15-Sep-20','16-Sep-20','17-Sep-20','18-Sep-20']].mean(axis=1)
But when using the following shorter code it throws syntax error:
Average_compact= df.loc[[0:8] ,['15-Sep-20':'18-Sep-20']]).mean(axis=1)
output: File "", line 1
Average_compact= df.loc[[0:8] ,['15-Sep-20':'18-Sep-20']].mean(axis=1)
^
SyntaxError: invalid syntax
Where am i doing wrong ?
Also how can i select the entire range of available rows i.e from 0 to last row without mentioning the end row number
can you try using iloc instead of loc:
df[column_list].iloc[row_index_list].mean(axis=0)