从与熊猫中另一个值接近的列列表中选择值

时间:2021-01-21 17:48:49

标签: python pandas dataframe numpy

我有以下数据框:

S0  S1  S2  S3  S4  S5... Price
10  15  18  12  18  19     16
55  45  44  66  58  45     64
77  84  62  11  61  44     20    

我想创建另一个列 Sup,其中存储一个低于 Price 的值。 预期结果:

S0  S1  S2  S3  S4  S5... Price  Sup
10  15  18  12  18  19     16    15
55  45  44  66  58  45     64    58
77  84  62  11  61  44     20    11

这是我一直在尝试的:

s = np.sort(df.filter(like='S').values, axis=1)
mask = (s*1.03) > df['Close'].values[:,None]
df['Sup'] = np.where(mask.any(1), s[np.arange(s.shape[0]),mask.argmax(1)], 0)

如果差异大于 3%,则说明工作不正确。请注意,s 列可能会有所不同,因此我想保留 np.sort(df.filter(like='S').values, axis=1)

将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

试试这个:

s = df.filter(like='S')

# compare to the price
mask = s.lt(df['Price'], axis=0)

# `where(mask)` places `NaN` where mask==False
# `max(1)` takes maximum along rows, ignoring `NaN`
# rows with all `NaN` returns `NaN` after `max`, 
# `fillna(0)` fills those with 0
df['Price'] = s.where(mask).max(1).fillna(0)

输出:

   S0  S1  S2  S3  S4  S5  Price
0  10  15  18  12  18  19   15.0
1  55  45  44  66  58  45   58.0
2  77  84  62  11  61  44   11.0

答案 1 :(得分:1)

也许这可以完成工作:

p = df['Price']
m = df.filter(like='S').lt(p, axis=0)
df['Sup'] = df[m].max(axis=1).astype(int)