zhr - 9 months ago 59

Python Question

I Have a multi-index dataframe as:

`df =`

sta1 sta2 ... stan

coef e p coef e p ... coef e p

t 3 0.1 0.001 2 0.1 0.001 1 0.1 0.01

u 3 0.2 0.01 3 0.01 0.01 2 0.1 0.001

v 2 0.1 0.001 4 0.1 0.1 2 0.001 0.001

I want to put a condition if p>0.01 then coef = nan. My results looks like:

`df =`

sta1 sta2 ... stan

coef e p coef e p ... coef e p

t 3 0.1 0.001 nan 0.1 0.1 1 0.1 0.01

u 3 0.2 0.01 3 0.01 0.01 nan 0.1 0.1

v 2 0.1 0.001 nan 0.1 0.1 2 0.001 0.001

I appreciate that if someone guide me. Thank you.

Answer Source

I tested this on my random data, please tell me if it works for your example:

```
for multindex in df.columns.values:
if multindex[1] == 'p':
df[(multindex[0], 'coef')] = np.where(df[multindex] > 0.01, np.nan, df[(multindex[0], 'coef')])
```