Here is how my df kind of looks like (with many more rows, and many more columns):
Index | WTG1 | WTG2 | WTG3 |
---|---|---|---|
1.5 | 61.25 | -7.57 | 7.18 |
2 | 19.69 | 25.95 | 28.67 |
2.5 | 59.51 | 81.22 | 78.22 |
3 | 131.81 | 154.07 | 142.92 |
My objective is to get:
Index | WTG1 | WTG2 | WTG3 | 25th Percentile | 75th Percentile | Mean |
---|---|---|---|---|---|---|
1.5 | 61.25 | -7.57 | 7.18 | (25th Percentile of 61.2, -7.57, 7.18) | (75th Percentile of 61.2, -7.57, 7.18) | (Avg. of 61.2, -7.57, 7.18) |
2 | 19.6 | 25.95 | 28.67 | (25th Percentile of 19.69, 25.95, 28.67) | (75th Percentile of 19.69, 25.95, 28.67) | (AVG. of 19.69, 25.95, 28.67) |
2.5 | 59.51 | 81.22 | 78.22 | (25th Percentile of 59.51, 81.22, 78.22) | (75th Percentile of 59.51, 81.22, 78.22) | (AVG. of 59.51, 81.22, 78.22) |
3 | 131.81 | 154.07 | 142.92 | (25th Percentile of 131.81, 154.07, 142.92) | (75th Percentile of 131.81, 154.07, 142.92) | (AVG. of 131.81, 154.07, 142.92) |
I have been looking for a long time now and the best I can do it :
df['mean'] = df[['WTG1','WTG2','WTG3'].mean(axis=1)
df['25th Percentile'] = np.nanpercentile(df[['WTG1','WTG2','WTG3']],25)
df['75th Percentile'] = np.nanpercentile(df[['WTG1','WTG2','WTG3']],75)
The mean seems to work, have not been checking the values yet though.
But the percentiles are the real issues here... it seems that nanpercentile function works only on columns. It returns the same value on every line (which I guess is the respective 25th and 75th percentile value but of the whole df) for both percentiles columns, which is not what I attend to do.
I was able to find some alternatives but could not adapt them to my need, as:
perc75 = np.vectorize(lambda x: np.percentile(x, 75))
df['75th_percentile'] = perc75(df['WTG01'].values)
which work but only for one column.
or
df['25th_percentile'] = df['WTG1','WTG2','WTG3'].apply(lambda x: np.percentile(x, 25))
which does not work...