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’.
>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)