仅保留具有特定十进制值的行

2024-06-06 14:15:19 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我脑子里有个屁,我不记得如何根据数字的小数点过滤掉数字

假设我的数据帧是-

dic = {'product':['Bread','Milk','Eggs','Water','OJ','Cereal','Coffee',
                    'Apples','Banana','Muffin'],
       'price':[3.89,2.99,4.00,0.69,1.99,2.39,5.00,0.99,0.98,1.50]}
df = pd.DataFrame(dic)
print(df)

输出-

  product  price
0   Bread   3.89
1    Milk   2.99
2    Eggs   4.00
3   Water   0.69
4      OJ   1.99
5  Cereal   2.39
6  Coffee   5.00
7  Apples   0.99
8  Banana   0.98
9  Muffin   1.50

我只想让价格以.99、.00和.50结尾

我期望的结果是——

  product  price
1    Milk   2.99
2    Eggs   4.00
4      OJ   1.99
6  Coffee   5.00
7  Apples   0.99
9  Muffin   1.50

应该知道怎么做,只是现在记不起来了


Tags: df数字productpriceeggsbananacoffeemuffin
2条回答

如果这些是简单的货币(美元)金额,您可以将十进制值转换为整数(以避免浮动比较,这可能会导致错误的答案),然后进行isin检查:

df[df['price'].mul(100).mod(100).astype(int).isin([0, 50, 99])]

  product  price
1    Milk   2.99
2    Eggs   4.00
4      OJ   1.99
6  Coffee   5.00
7  Apples   0.99
9  Muffin   1.50

根据我的测试,这是两个中速度更快的一个


另一个带有np.isclose的选项:

df[np.logical_or.reduce([
    np.isclose(df['price'].mod(1), d) for d in [0, .99, .5]])]

  product  price
1    Milk   2.99
2    Eggs   4.00
4      OJ   1.99
6  Coffee   5.00
7  Apples   0.99
9  Muffin   1.50

您可以这样做:

dic = {'product':['Bread','Milk','Eggs','Water','OJ','Cereal','Coffee','Apples','Banana','Muffin'],
       'price':[3.89,2.99,4.00,0.69,1.99,2.39,5.00,0.99,0.98,1.50]}


for price in dic['price']:
    if str(price).split('.')[1] not in ['99','5'] and int(price)!=price:
        dic['product'].pop(dic['price'].index(price)) # Remove the product that aligns with the unwanted price
        dic['price'].remove(price) # Remove the price

print(dic)

输出:

{'product': ['Milk', 'Eggs', 'OJ', 'Coffee', 'Apples', 'Muffin'],
 'price': [2.99, 4.0, 1.99, 5.0, 0.99, 1.5]}

相关问题 更多 >