Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

pandas mean of n columns

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 by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #pandas #columns
ADD COMMENT
Topic
Name
1+1 =