'How do I calculate the standard deviation of columns in a table while excluding a large number of rows? Then produce a table of these results?
e.g Standard deviation of column 'Ex_Rate_Fr' excluding rows 6-11
Date Ind_Prod_Fr Ex_Rate_Fr Cpi_Fr Cpi_US Log_Ex_Rate_Fr
0 1960-01 37.698844 4.937060 10.383900 13.666246 1.596770
1 1960-02 38.582145 4.937060 10.383900 13.666246 1.596770
2 1960-03 38.718038 4.937060 10.383900 13.666246 1.596770
3 1960-04 39.601339 4.937060 10.383900 13.666246 1.596770
4 1960-05 39.635312 4.937060 10.386551 13.666246 1.596770
5 1960-06 39.635312 4.937060 10.389201 13.574526 1.596770
6 1960-07 36.611704 4.937060 10.391851 13.574526 1.596770
7 1960-08 37.472357 4.937060 10.487262 13.574526 1.596770
8 1960-09 38.333009 4.937060 10.503163 13.574526 1.596770
9 1960-10 40.586560 4.937060 10.519066 13.666246 1.596770
10 1960-11 41.956809 4.937060 10.550869 13.666246 1.596770
11 1960-12 41.820917 4.937060 10.574724 13.666246 1.596770
Solution 1:[1]
You can have a list of tuples, where each tuple represents a start and stop position (both inclusive), and use ranges and nested list-comprehension to pass them to drop before calling std:
ranges = [
(1,2),
(6,11),
]
stddev = df['Ex_Rate_Fr'].drop([i for x in ranges for i in range(x[0], x[1] + 1)]).std()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
