函数之间的数据帧显示不正确的结果

2024-09-30 14:34:25 发布

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

我想打印那些item_pricefinal介于item_price1item_price2之间的行

这里是我正在运行的代码和数据集-

import pandas as pd

pd.set_option('display.max_columns', None)  # Number of columns to be displayed
pd.set_option('display.width', None)  # Max table width to display
pd.set_option('display.max_rows', None)  # Max number of rows
pd.set_option('mode.chained_assignment', None)  # Turn off SettingWithCopyWarning warning

data_list = <<Please read from here - https://1drv.ms/t/s!Aiw4HhkSppuhET8gSypP?e=y71rot
data_df = pd.DataFrame(data_list)
data_df.columns = ['item_date', 'item_price1', 'item_price2', 'item_pricefinal', 'itempricefinal-itemprice1', 'itempricefinal-itemprice2']
mask = data_df['item_pricefinal'].between(data_df['item_price1'], data_df['item_price2'])

对于每一行,输出都为false,尽管存在此条件保持良好的行

示例-在以下条目中,0.890624368082415介于0.90588和;0.86638,但是between函数认为它不是,并在输出中打印false

['2020-03-09 00:00:00', 0.90588, 0.86638, 0.8906243680824152, 152.55631917584833, 242.44368082415147]

请允许我请求帮助以发现错误


Tags: columnsoftononedfdatadisplayitem
2条回答

data_df['item_pricefinal'].between(a,b)data_df['item_pricefinal'].between(b,a)之间存在差异。顺序(a,b)或(b,a)很重要

看起来item_price2item_price1低。所以正确的代码应该是

data_df['item_pricefinal'].between(data_df['item_price2'],data_df['item_price1'])

pd.between(left, right)left is left boundaryright is right boundary

在这种情况下,您的示例将起作用

mask = data_df['item_pricefinal'].between(
    data_df['item_price2'], data_df['item_price1'])

相关问题 更多 >