英文:
Using for loop for filtering in pandas dataframe
问题
我有一个名为'final'的大型数据集,我有一个名为'bn_stocks'的列表要进行筛选。筛选必须应用于'final'(数据集)的"SYMBOL"列。我尝试使用for循环,但只得到了列表的最后一个项目被筛选。
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]
当我运行上面的代码时,我只得到了最后一个元素'PNB'被筛选,而我需要筛选列表'bn_stocks'中的所有元素。
英文:
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'.
答案1
得分: 2
使用pandas
的目的是利用其功能,在99%的用例中,您不使用for-loop
来处理数据框。
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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论