Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using for loop for filtering in pandas dataframe

I have a large dataset ‘final’ and I have item to filter in a list named ‘bn_stocks’. The filtering has to be applied on "SYMBOL" column of ‘final'(dataset). I tried using for loop but I only got last item of the list filtered.

bn_stocks = ['HDFCBANK','ICICIBANK','AXISBANK','KOTAKBANK','SBIN','INDUSINDBK','BANKBARODA','AUBANK','FEDERALBANK',
             'IDFCFIRSTB','BANDHANBK','PNB']
for bn_stock in bn_stocks :
    filt4 = final['SYMBOL'] == bn_stock
    a = final[filt4]

when I run the above code I only get the last element filtered ‘PNB’ , whereas I require it to filter all the elements in the list ‘bn_stocks’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The purpose of using pandas, is to use its power, in 99% of usecases you don’t use a for-loop to do something with a dataframe

import pandas as pd

bn_stocks = ['HDFCBANK', 'ICICIBANK', 'AXISBANK', 'KOTAKBANK', 'SBIN',
             'INDUSINDBK', 'BANKBARODA', 'AUBANK', 'FEDERALBANK',
             'IDFCFIRSTB', 'BANDHANBK', 'PNB']

final = pd.DataFrame({
    "value": [1, 2, 3, 4, 5],
    "SYMBOL": ["AXISBANK", "A", "B", "AUBANK", "PNB"]
})

filtered = final[final["SYMBOL"].isin(bn_stocks)]
print(filtered)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading