Skip to content Skip to sidebar Skip to footer

Pandas Dataframe: Add Mean And Std Columns To Every Column

I would like to add a mean and a std column to every column of a dataframe. Unfortunately my code replaces the original columns by the mean and std ones. np.random.seed(50) df = pd

Solution 1:

In [18]: res = df.rolling(5).agg(['mean','std'])

In [19]: res.columns = res.columns.map('_'.join)

In [54]: cols = np.concatenate(list(zip(df.columns, res.columns[0::2], res.columns[1::2])))

In [55]: cols
Out[55]:
array(['A', 'A_mean', 'A_std', 'B', 'B_mean', 'B_std', 'C', 'C_mean', 'C_std'],
      dtype='<U6')

In [56]: res.join(df).loc[:, cols]
Out[56]:
    A  A_mean     A_std  B  B_mean     B_std  C  C_mean     C_std
0   0     NaN       NaN  0     NaN       NaN  1     NaN       NaN
1   4     NaN       NaN  6     NaN       NaN  5     NaN       NaN
2   6     NaN       NaN  6     NaN       NaN  5     NaN       NaN
3   2     NaN       NaN  7     NaN       NaN  4     NaN       NaN
4   3     3.0  2.236068  6     5.0  2.828427  4     3.8  1.643168
5   1     3.2  1.923538  5     6.0  0.707107  0     3.6  2.073644
6   6     3.6  2.302173  3     5.4  1.516575  2     3.0  2.000000
7   3     3.0  1.870829  3     4.8  1.788854  3     2.6  1.673320
8   2     3.0  1.870829  0     3.4  2.302173  3     2.4  1.516575
9   2     2.8  1.923538  0     2.2  2.167948  3     2.2  1.303840
10  0     2.6  2.190890  0     1.2  1.643168  7     3.6  1.949359
11  3     2.0  1.224745  8     2.2  3.492850  7     4.6  2.190890
12  4     2.2  1.483240  4     2.4  3.577709  0     4.0  3.000000
13  0     1.8  1.788854  3     3.0  3.316625  3     4.0  3.000000
14  1     1.6  1.816590  4     3.8  2.863564  5     4.4  2.966479
15  7     3.0  2.738613  0     3.8  2.863564  3     3.6  2.607681
16  5     3.4  2.880972  6     3.4  2.190890  1     2.4  1.949359
17  4     3.4  2.880972  4     3.4  2.190890  4     3.2  1.483240
18  5     4.4  2.190890  4     3.6  2.190890  6     3.8  1.923538
19  3     4.8  1.483240  0     2.8  2.683282  5     3.8  1.923538
20  8     5.0  1.870829  3     3.4  2.190890  6     4.4  2.073644
21  2     4.4  2.302173  8     3.8  2.863564  8     5.8  1.483240
22  5     4.6  2.302173  4     3.8  2.863564  7     6.4  1.140175
23  8     5.2  2.774887  4     3.8  2.863564  4     6.0  1.581139
24  2     5.0  3.000000  1     4.0  2.549510  8     6.6  1.673320
25  7     4.8  2.774887  1     3.6  2.880972  5     6.4  1.816590
26  8     6.0  2.549510  3     2.6  1.516575  3     5.4  2.073644
27  5     6.0  2.549510  3     2.4  1.341641  6     5.2  1.923538
28  8     6.0  2.549510  6     2.8  2.049390  0     4.4  3.049590
29  8     7.2  1.303840  2     3.0  1.870829  1     3.0  2.549510

Post a Comment for "Pandas Dataframe: Add Mean And Std Columns To Every Column"