Search code examples
pandasgroup-by

Pandas groupby and filter with multiple conditions


        Date    Symbol   Open   High    Low  Close
0 2023-05-31  GEDIK.IS   7.90   8.01   7.77   7.87
1 2023-06-01  GEDIK.IS   7.92   8.20   7.89   8.14
2 2023-05-31  MIPAZ.IS   7.87   7.90   7.74   7.84
3 2023-06-01  MIPAZ.IS   7.84   8.06   7.80   8.05
4 2023-05-31  SUNTK.IS  36.20  37.52  35.48  37.00
5 2023-06-01  SUNTK.IS  37.20  38.30  36.60  38.30
6 2023-05-31  VANGD.IS   7.26   7.36   6.95   7.08
7 2023-06-01  VANGD.IS   7.09   7.63   6.92   7.48

I want to filter stocks from this dataframe where current day (06-01) has a positive candle (Close>Open) and creates a LL with the previous day. In this example it should return VANGD only. I can filter LLs by

df.groupby(['Symbol']).apply(lambda x: x[x["Low"] > x["Low"].shift(-1)])

However this only returns first row of the matching stock. I need to get entire group.

                 Date    Symbol  Open  High   Low  Close
Symbol
VANGD.IS 6 2023-05-31  VANGD.IS  7.26  7.36  6.95   7.08

Than I can filter stocks with positive candle. Also there are only 4 stocks in this example. Keep in mind that original df have nearly 500 stocks to be filtered.

Many thanks.


Solution

  • It looks like you want to extract the matching Symbol names then .isin() to find all corresponding rows.

    If the Symbols are all adjacent, you can replace the .groupby().apply by also comparing the shifted Symbol:

    LLs = df.loc[
       (df['Symbol'] == df['Symbol'].shift(-1)) & 
       (df['Low'] > df['Low'].shift(-1)), 
       'Symbol'
    ]
    
    df[df['Symbol'].isin(LLs)]
    
            Date    Symbol  Open  High   Low  Close
    6 2023-05-31  VANGD.IS  7.26  7.36  6.95   7.08
    7 2023-06-01  VANGD.IS  7.09  7.63  6.92   7.48