如何为数据框的每一行提取两个最大值?
Date gld tlt qqq
2012-01-31 NaN NaN NaN
2012-02-29 -0.064527 0.391660 -0.071191
2012-03-31 -0.064527 0.391660 -0.071191
2012-04-30 0.025958 -0.009515 0.145537
2012-05-31 -0.292595 -0.395539 0.538963
2012-06-30 -0.292595 -0.395539 0.538963
2012-07-31 0.055247 0.067361 0.235317
2012-08-31 0.441790 0.280636 -0.070989
2012-09-30 0.441790 0.280636 -0.070989
2012-10-31 -0.236148 -0.126485 -0.021196
2012-11-30 -0.021589 0.032538 0.083634
我想创建一个新的,每个日期都有一个或两个最高值!
我将有一个这样的新数据框:
Date gld tlt qqq
2012-02-29 nan 0.391660 nan
2012-03-31 nan 0.391660 nan
2012-04-30 nan nan 0.145537
2012-05-31 nan nan 0.538963
2012-08-31 0.441790 nan nan
答案 0 :(得分:0)
尝试一下:
def func(x):
x[[c for c in x.index.to_list() if c != np.argmax(x)]] = np.NaN
return x
df[['gld', 'tlt', 'qqq']].apply(lambda x: func(x), axis=1)
答案 1 :(得分:0)
如果您要显示的是每行顶部的l
值,我建议:
l = 2
df.where(df.rank(axis = 1, ascending=False, method='dense') <= l)
输出(带有l=2
):
gld tlt qqq
Date
2012-02-29 -0.064527 0.391660 NaN
2012-03-31 -0.064527 0.391660 NaN
2012-04-30 0.025958 NaN 0.145537
2012-05-31 -0.292595 NaN 0.538963
2012-06-30 -0.292595 NaN 0.538963
2012-07-31 NaN 0.067361 0.235317
2012-08-31 0.441790 0.280636 NaN
2012-09-30 0.441790 0.280636 NaN
2012-10-31 NaN -0.126485 -0.021196
2012-11-30 NaN 0.032538 0.083634