Python 从元组中筛选数据帧

Python 从元组中筛选数据帧,python,pandas,Python,Pandas,结果 现在,上面的数据帧必须通过元组的AB_col列表进行过滤。我试过类似的东西 AB_col = [(0,230), (10,215), (15, 200), (20, 185), (40, 177), (0,237), (10,222), (15, 207), (20, 192), (40, 184)] sales = [{'account': 'Jones LLC', 'A': 0, 'B': 230, 'C': 140}, {'ac

结果

现在,上面的数据帧必须通过元组的AB_col列表进行过滤。我试过类似的东西

AB_col = [(0,230), (10,215), (15, 200), (20, 185), (40, 177), 
                (0,237), (10,222), (15, 207), (20, 192), (40, 184)]

sales = [{'account': 'Jones LLC', 'A': 0, 'B': 230, 'C': 140},
         {'account': 'Alpha Co',  'A': 20, 'B': 192, 'C': 215},
         {'account': 'Blue Inc',  'A': 50,  'B': 90,  'C': 95 }]
df = pd.DataFrame(sales)
print df
但是它不起作用,如何将上面的数据帧过滤到下面这样的数据帧


您需要创建
元组的
系列

df[df["A","B"].zip.isin(AB_col)]
备选方案:

df = df[df[["A","B"]].apply(tuple, 1).isin(AB_col)]
或者您可以比较由以下人员创建的
多索引

或者创建自己的多索引和过滤器:

df = df[df.set_index(['A','B']).index.isin(AB_col)]

df = df[df.set_index(['A','B']).index.isin(AB_col)]
df = df[pd.MultiIndex.from_arrays([df['A'], df['B']]).isin(AB_col)]
print (df)
    A    B    C    account
0   0  230  140  Jones LLC
1  20  192  215   Alpha Co