有了Pandas DataFrame,我想过滤所有行,使其在特定列的对应值位于某个预定义的unique_vals
向量中,例如:
test_df = pd.DataFrame(['a','b','c','d','e'], columns = ['Test'])
预定义的unique_vals
向量:
unique_vals = ['a','b','d']
已处理的数据帧由原始数据帧中的行0、1、3组成,这些行在“测试”列中包含值“ a”,“ b”,“ d”。
一种方法是:
result_df = test_df[[row in unique_vals for row in test_df['Test']]]
使用pandas的DataFrame内置函数是否有更优雅的方法来实现这一目标?