我有一个多索引数据框:
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
我想设置一个条件,如果 p>0.01 则 coef = nan。我的结果如下:
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
如果有人指导我,我将不胜感激。谢谢。
请您参考如下方法:
我在我的随机数据上测试了这个,请告诉我它是否适用于你的例子:
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')])
