Incorrect results when applying solution to real data

后端 未结 2 667
一向
一向 2021-01-17 17:37

I\'ve tried to apply the solution provided in this question to my real data: Selecting rows in a MultiIndexed dataframe. Somehow I cannot get the results it should give. I\'

2条回答
  •  一整个雨季
    2021-01-17 17:58

    According to the logic from your comment you are looking for rows that have every value in columns 2012,2013,2014,2015 less than 0 or have a cumulative sum less than 0. Since the first condition will always be true when the second condition is true you just test for the second condition.

    cols = ['2012', '2013', '2014', '2015']
    df.loc[(df[cols].cumsum(axis=1) < 0).all(axis=1), cols]
    
         2012   2013   2014   2015
    1   -6.74  -1.22   1.58  -0.42
    3   -3.14  -2.48  -0.02  -4.78
    4   -9.40 -11.20   0.68  12.04
    7   -3.12  -5.74   0.84   1.94
    8  -10.14 -12.24 -11.10  15.20
    11 -10.04 -10.60  -5.56  -8.44
    12  -7.30   5.96 -12.58  -6.86
    15 -10.24  -4.16   5.46 -14.00
    

    Let me know in the comments if this is not what you want.

提交回复
热议问题