检查一列中的值是否在Python中另一列的间隔值中

时间:2018-12-29 05:35:37

标签: python pandas

我有以下数据框:

import pandas as pd
import numpy as np
data = {
    "index": [1, 2, 3, 4, 5],
    "v1": [1100, 1776, 1228, 1640, np.NaN],
    "v2": [1000, 1805, 1231, 1425, 1800],
    "result": ['Y', 'Y', 'Y', 'N', 'N']
}
df = pd.DataFrame.from_dict(data)
print(df)

我想检查第v1列是否在第v2列的间隔内±100(或[v2 * 0.95,v2 * 1.05])。我如何在Python中做到这一点?谢谢你的帮助。这是我的最终结果示例:

    index    v1      v2    result
0      1  1100.0    1000      Y
1      2  1776.0    1805      Y
2      3  1228.0    1231      Y
3      4  1640.0    1425      N
4      5     NaN    1800      N

2 个答案:

答案 0 :(得分:2)

对于v1 in [v2*0.95, v2*1.05],请使用:

df['res'] = ''
for idx, val in enumerate(df.itertuples()):
    if df.loc[idx,'v1'] > df.loc[idx,'v2'] * 0.95 and df.loc[idx,'v1'] < df.loc[idx,'v2'] * 1.05:
        df.loc[idx, 'res'] = 'Yes'
    else:
        df.loc[idx, 'res'] = 'No'


+---+-------+--------+--------+------+-----+
|   | index | result |   v1   |  v2  | res |
+---+-------+--------+--------+------+-----+
| 0 |     1 | Y      | 1100.0 | 1000 | No  |
| 1 |     2 | Y      | 1776.0 | 1805 | Yes |
| 2 |     3 | Y      | 1228.0 | 1231 | Yes |
| 3 |     4 | N      | 1640.0 | 1425 | No  |
| 4 |     5 | N      | NaN    | 1800 | No  |
+---+-------+--------+--------+------+-----+

答案 1 :(得分:2)

只需使用

(df.v1-df.v2).abs().le(100).map({True:'Yes',False:'No'})
Out[60]: 
0    Yes
1    Yes
2    Yes
3     No
4     No
dtype: object