Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python数据帧-如何跟踪最近的最低值_Python 3.x_Dataframe - Fatal编程技术网

Python 3.x Python数据帧-如何跟踪最近的最低值

Python 3.x Python数据帧-如何跟踪最近的最低值,python-3.x,dataframe,Python 3.x,Dataframe,我有一个数据框,如下所示,在一个名为data的数据框中 SN value 1 895.1 2 900.94 3 920.26 4 918.9 5 927.23 6 919.32 7 923.33 8 896.42 9 898.72 10 881.03 11 879.56 12 882.68 13 879.13 14 901.05 15 905.84 16 932.68 17 940.74 此代码仅适用于一个通行证。但是当第二次翻转时,它不会更

我有一个数据框,如下所示,在一个名为data的数据框中

SN value 1 895.1 2 900.94 3 920.26 4 918.9 5 927.23 6 919.32 7 923.33 8 896.42 9 898.72 10 881.03 11 879.56 12 882.68 13 879.13 14 901.05 15 905.84 16 932.68 17 940.74 此代码仅适用于一个通行证。但是当第二次翻转时,它不会更新。请参阅第14行

我的代码输出

SN value new_low is_new_low 1 895.1 NaN FALSE 2 900.94 895.1 FALSE 3 920.26 895.1 FALSE 4 918.9 895.1 FALSE 5 927.23 895.1 FALSE 6 919.32 895.1 FALSE 7 923.33 895.1 FALSE 8 896.42 895.1 FALSE 9 898.72 895.1 FALSE 10 881.03 881.03 TRUE 11 879.56 879.56 TRUE 12 882.68 882.68 TRUE 13 879.13 879.13 TRUE 14 901.05 895.1 FALSE #Here it should be 879.13. But 895.1 is coming 15 905.84 895.1 FALSE 16 850.2 895.1 TRUE 17 870.14 895.1 TRUE SN值新低为新低 1895.1 NaN错误 2900.94895.1错误 3920.26895.1错误 4918.9895.1错误 5927.23895.1错误 6919.32895.1错误 7923.33 895.1错误 8 896.42 895.1错误 9898.72895.1错误 10 881.03 881.03正确 11879.56879.56正确 12882.68 882.68正确 13879.13879.13正确 14 901.05 895.1假#这里应该是879.13。但是895.1即将到来 15 905.84 895.1错误 16850.2895.1正确 17 870.14 895.1正确 所需输出为

SN value new_low is_new_low 1 895.1 NaN FALSE 2 900.94 895.1 FALSE 3 920.26 895.1 FALSE 4 918.9 895.1 FALSE 5 927.23 895.1 FALSE 6 919.32 895.1 FALSE 7 923.33 895.1 FALSE 8 896.42 895.1 FALSE 9 898.72 895.1 FALSE 10 881.03 881.03 TRUE 11 879.56 879.56 TRUE 12 882.68 882.68 TRUE 13 879.13 879.13 TRUE 14 901.05 879.13 FALSE 15 905.84 879.13 FALSE 16 850.2 850.2 TRUE 17 870.14 870.14 FALSE SN值新低为新低 1895.1 NaN错误 2900.94895.1错误 3920.26895.1错误 4918.9895.1错误 5927.23895.1错误 6919.32895.1错误 7923.33 895.1错误 8 896.42 895.1错误 9898.72895.1错误 10 881.03 881.03正确 11879.56879.56正确 12882.68 882.68正确 13879.13879.13正确 14901.05 879.13假 15 905.84 879.13假 16850.2850.2正确 17 870.14 870.14错误 如何实现这一点?

您可以使用df.itertuples()迭代所有行并更新新的\u low值。对于每一行,检查您的值是否小于前一行中的新值。如果是,将新的低值更新为当前值,否则将新的低值设置为上一列的新低值

for row in data.itertuples():
    if(row.Index):
        if data.at[row.Index,"value"] < data.at[row.Index -1,"new_low"]:
            data.at[row.Index,"new_low"] = data.at[row.Index,"value"]
        else:
            data.at[row.Index,"new_low"] = data.at[row.Index -1,"new_low"]
如果您想使用numpy获得更好的性能,可以使用:

np.minimum.accumulate(df.value)
df['new_low'] = df['value'].cummin()
np.minimum.accumulate(df.value)