Pandas Mean Of N Columns

[Solved] Pandas Mean Of N Columns | Perl - Code Explorer | yomemimo.com
Question : pandas mean of n columns

Answered by : frail-flatworm-zzx20ocnfkp2

N = 9
df['new'] = df['col2'].shift(-N+1)
df.loc[0, 'new'] = df.iloc[:N, df.columns.get_loc('col2')].mean()
print (df) col2 desired_output new
0 14.375 14.261111 14.261111
1 14.350 14.175000 14.175000
2 14.300 14.125000 14.125000
3 14.350 14.150000 14.150000
4 14.300 13.974000 13.974000
5 14.225 14.074000 14.074000
6 14.175 14.099000 14.099000
7 14.150 13.949000 13.949000
8 14.125 13.899000 13.899000
9 14.175 13.849000 13.849000
10 14.125 13.749000 13.749000
11 14.150 13.774000 13.774000
12 13.974 NaN NaN
13 14.074 NaN NaN
14 14.099 NaN NaN
15 13.949 NaN NaN
16 13.899 NaN NaN
17 13.849 NaN NaN
18 13.749 NaN NaN
19 13.774 NaN NaN

Source : https://stackoverflow.com/questions/55275131/how-to-take-average-first-10-150-rows-of-column-and-use-average-value-as-first-v | Last Update : Tue, 01 Jun 21

Answers related to pandas mean of n columns

Code Explorer Popular Question For Perl