Python 熊猫中如何根据多列快速选择数据帧

Python 熊猫中如何根据多列快速选择数据帧,python,pandas,performance,dataframe,Python,Pandas,Performance,Dataframe,我想按多列值筛选行 例如,给定以下数据帧 import pandas as pd df = pd.DataFrame({"name":["Amy", "Amy", "Amy", "Bob", "Bob",], "group":[1, 1, 1, 1, 2], "place":['a', 'a', "a", 'b', 'b'], "y":[1, 2, 3, 1, 2] }) prin

我想按多列值筛选行

例如,给定以下数据帧

import pandas as pd
df = pd.DataFrame({"name":["Amy", "Amy", "Amy", "Bob", "Bob",],
                  "group":[1, 1, 1, 1, 2],
                   "place":['a', 'a', "a", 'b', 'b'],
                    "y":[1, 2, 3, 1, 2]
})

print(df)
原始数据帧:

name  group place  y
0  Amy      1     a  1
1  Amy      1     a  2
2  Amy      1     a  3
3  Bob      1     b  1
4  Bob      2     b  2
我想在
selectRow
中选择满足列组合[
name
group
place
]的样本

selectRow=[[“艾米”,1,“a”],[“艾米”,2,“b”]]

然后,
预期的数据帧是:

  name  group place  y
0  Amy      1     a  1
1  Amy      1     a  2
2  Amy      1     a  3
我已经试过了,但我的方法效率不高,而且运行了很长时间,特别是当原始数据帧中有很多样本时

我的简单方法:

newdf = pd.DataFrame({})
for item in (selectRow):
    print(item)
    tmp = df.loc[(df['name'] == item[0]) & (df['group'] == item[1]) &  (df['place'] == item[2])]
    newdf = newdf.append(tmp)
newdf = newdf.reset_index( drop = True)
newdf.tail()
print(newdf)

希望有一个有效的方法来实现它

尝试使用
isin

print(df[df['name'].isin(list(zip(*selectRow))[0]) & df['group'].isin(list(zip(*selectRow))[1]) & df['place'].isin(list(zip(*selectRow))[2])])