Removing the first rows before a specific value - pandas

后端 未结 2 433
心在旅途
心在旅途 2021-01-21 05:08

I am trying to remove all rows before an initial value for a group. For instance, if my max_value = 250, then all rows for a group before that value should be remov

2条回答
  •  孤独总比滥情好
    2021-01-21 05:15

    This should do the trick:

    df[df.groupby('Asset')['Monthly Value'].apply(lambda x: x.gt(max_value).cumsum().ne(0))]
    

    Yields:

              date    Asset  Monthly Value
    2   2019-03-01  Asset A            300
    3   2019-04-01  Asset A            400
    4   2019-01-01  Asset A            500
    5   2019-02-01  Asset A            600
    8   2019-01-01  Asset B            300
    9   2019-02-01  Asset B            200
    10  2019-03-01  Asset B            300
    11  2019-04-01  Asset B            200
    

    Additionally, if you store your max values in a dictionary like max_value = {'Asset A': 250, 'Asset B': 250}, you can do the following to achieve the same result:

    df[df.groupby('Asset')['Monthly Value'].apply(lambda x: x.gt(max_value[x.name]).cumsum().ne(0))]
    

提交回复
热议问题