我有两只Pandas DataFrame, struct 相同.数据帧B是数据帧A的子集.
我要筛选DataFrame B,只有当价格值出现在DataFrame A,or it is within 1% of a value in DataFrame A中时.
例如,即使不存在确切的价格,如果A中有一行的价格为+/-1%,我也希望保持该值.
数据帧A:
index price
0 20.23
1 10.34
2 5.28
3 12.25
4 12.32
数据帧B:
index price
0 0.23
1 10.34
2 5.26
过滤所需的结果:
index price
0 10.34
1 5.26
import pandas as pd
dfA = pd.DataFrame({'index': [0, 1, 2, 3, 4],
'price': [20.23, 10.34, 5.28, 12.25, 12.32]})
dfB = pd.DataFrame({'index': [0, 1, 2],
'price': [0.23, 10.34, 5.26]})
下面的内容只会给我提供完全匹配的信息.
dfB[dfB['price'].isin(dfA['price'])]