Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.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 如何在pandas数据帧中获取最小值之后的所有记录_Python_Pandas_Search - Fatal编程技术网

Python 如何在pandas数据帧中获取最小值之后的所有记录

Python 如何在pandas数据帧中获取最小值之后的所有记录,python,pandas,search,Python,Pandas,Search,如何获取所有记录(在最小值之后) 最小值是7,我想得到它之后的所有记录 Date Value 2020-03-11 20 2020-02-25 10 2020-02-20 7.5 2020-02-15 7 2020-01-10 10 2019-12-07 15 我试过了 min1=dfs[i].loc[dfs[i]['Value']

如何获取所有记录(在最小值之后)

最小值是7,我想得到它之后的所有记录

Date             Value    
2020-03-11       20 
2020-02-25       10
2020-02-20       7.5      
2020-02-15       7        
2020-01-10       10       
2019-12-07       15       
我试过了

min1=dfs[i].loc[dfs[i]['Value'] == dfs[i]['Value'].min()]['Date'].values[0]
dfs[i][min1:]
但这引起了一个错误 'TypeError:无法使用的这些索引器[2020-02-12T00:00:00.000000000]对进行切片索引'


注意:我有一个数据帧列表。

您想使用
shift
获取移位序列,然后将其与
min
进行比较:

df[df.Value.shift()==df.Value.min()]
输出:

         Date  Value
4  2020-01-10   10.0
4    10.0
Name: Value, dtype: float64
如果您只想获得

df.loc[df.Value.shift()==df.Value.min(), 'Value']
输出:

         Date  Value
4  2020-01-10   10.0
4    10.0
Name: Value, dtype: float64

您希望使用
shift
获取移位序列,然后将其与
min
进行比较:

df[df.Value.shift()==df.Value.min()]
输出:

         Date  Value
4  2020-01-10   10.0
4    10.0
Name: Value, dtype: float64
如果您只想获得

df.loc[df.Value.shift()==df.Value.min(), 'Value']
输出:

         Date  Value
4  2020-01-10   10.0
4    10.0
Name: Value, dtype: float64

通过执行此操作,可以获得第一个最小值之后的所有行

df.loc[df['Value'].idxmin():,]

编辑:根据下面的注释获取最小值后的行,不包括最小值
df.loc[df['Value'].idxmin()+1:,]


请注意,我假设索引是连续整数,如果不是,请重置索引。如果您的索引不是整数且非单调的,请检查下面的@Valdi_Bo answer

您可以通过此操作获得第一个最小值之后的所有行

df.loc[df['Value'].idxmin():,]

编辑:根据下面的注释获取最小值后的行,不包括最小值
df.loc[df['Value'].idxmin()+1:,]


请注意,我假设索引是连续整数,如果不是,请重置索引。如果您的索引不是整数且非单调的,请检查下面的@Valdi_Bo answer

假设数据帧包含:

          Date  Value
A1  2020-03-11   20.0
H2  2020-02-25   10.0
E3  2020-02-20    7.5
C4  2020-02-15    7.0
B5  2020-01-10   10.0
M6  2019-12-07   15.0
为了证明我的方法在所有可能的情况下都有效,我故意 选择了非数值和非单调索引,但索引必须是唯一的

请注意:

  • idxm=df.Value.idxmin()
    值-
    C4

  • df.loc[:idxm].index
    检索索引的“初始”部分, 最多到“最小”行(包括)-
    索引(['A1','H2','E3','C4'],dtype='object')

  • df.index.difference(df.loc[:idxm.index)
    检索其他部分 索引的
    索引(['B5','M6'],dtype='object')

  • 因此得出结论,正确的表达是:

    df.loc[df.index.difference(df.loc[:idxm].index)]
    
              Date  Value
    B5  2020-01-10   10.0
    M6  2019-12-07   15.0
    
    此表达式的结果是:

    df.loc[df.index.difference(df.loc[:idxm].index)]
    
              Date  Value
    B5  2020-01-10   10.0
    M6  2019-12-07   15.0
    
    其他可能的解决办法:

    df.loc[df.Value.shift().idxmin():]
    

    假设数据帧包含:

              Date  Value
    A1  2020-03-11   20.0
    H2  2020-02-25   10.0
    E3  2020-02-20    7.5
    C4  2020-02-15    7.0
    B5  2020-01-10   10.0
    M6  2019-12-07   15.0
    
    为了证明我的方法在所有可能的情况下都有效,我故意 选择了非数值和非单调索引,但索引必须是唯一的

    请注意:

  • idxm=df.Value.idxmin()
    值-
    C4

  • df.loc[:idxm].index
    检索索引的“初始”部分, 最多到“最小”行(包括)-
    索引(['A1','H2','E3','C4'],dtype='object')

  • df.index.difference(df.loc[:idxm.index)
    检索其他部分 索引的
    索引(['B5','M6'],dtype='object')

  • 因此得出结论,正确的表达是:

    df.loc[df.index.difference(df.loc[:idxm].index)]
    
              Date  Value
    B5  2020-01-10   10.0
    M6  2019-12-07   15.0
    
    此表达式的结果是:

    df.loc[df.index.difference(df.loc[:idxm].index)]
    
              Date  Value
    B5  2020-01-10   10.0
    M6  2019-12-07   15.0
    
    其他可能的解决办法:

    df.loc[df.Value.shift().idxmin():]
    

    df.loc[[df['value'].idxmin()]:,]
    这将为您提供最小值之后的所有值。无法编辑之前的注释,因此在更正额外括号后再次发布答案
    df.loc[df['value'].idxmin():,]
    df.loc[[df['value'].idxmin()]:,]
    这将为您提供最小值之后的所有值无法编辑上一条注释,因此在更正额外括号后再次发布答案
    df.loc[df['value'].idxmin():,]
    此表达式检索从“最小”行开始的行,而OP希望在这行之后有行。非常感谢,这正是我要查找的。此表达式检索从“minimal”行开始的行,而OP希望在这行之后有行。非常感谢,这正是我要查找的。非常感谢,它帮助我获得了解决方案。我对df.loc[df.index.difference(df.loc[idxm:].index)]做了一个小改动,得到了A1、H2和E3。非常感谢,它帮助我得到了解决方案。我对df.loc[df.index.difference(df.loc[idxm:].index)]做了一个小改动,得到了A1、H2和E3。