英文:
In python/pandas, how to filter the dataframe whice condition stored in list
问题
在Python/Pandas中,如何使用存储在列表中的条件来过滤DataFrame?
以下代码ori_data[ori_data['category'] in ['a','d','f']]
无法工作
import pandas as pd
ori_data = pd.DataFrame({'category':['a','a','b','c','d','f']})
filtered_data = ori_data[ori_data['category'].isin(['a','d','f'])]
英文:
in python/pandas, how to filter the dataframe the condition stored in list ?
Below code ori_data[ori_data['category'] in ['a','d','f']]
can't work
import pandas as pd
ori_data = pd.DataFrame({'category':['a','a','b','c','d','f']})
filtered_data = ori_data[ori_data['category'] in ['a','d','f']]
答案1
得分: 1
如果我理解你的问题正确,你只需要使用.isin()
。
import pandas as pd
ori_data = pd.DataFrame({'category': ['a', 'a', 'b', 'c', 'd', 'f']})
filtered_data = ori_data[ori_data['category'].isin(['a', 'd', 'f'])]
英文:
If I understood your question correctly, all you need to do is use .isin()
.
import pandas as pd
ori_data = pd.DataFrame({'category': ['a', 'a', 'b', 'c', 'd', 'f']})
filtered_data = ori_data[ori_data['category'].isin(['a', 'd', 'f'])]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论