zhr zhr - 15 days ago 7
Python Question

put condition in pandas dataframe (multi-index)?

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

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')])
Comments