过滤数据框行

时间:2020-05-11 15:32:52

标签: python pandas

有了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内置函数是否有更优雅的方法来实现这一目标?

0 个答案:

没有答案
相关问题