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
result
Now the above dataframe has to be filtered by the AB_col list of tuples. I tried something like
df[df["A","B"].zip.isin(AB_col)]
But it did not work, How to filter the above dataframe to the one like below
You need create Series
of tuples
:
df = df[df[["A","B"]].apply(tuple, 1).isin(AB_col)]
Alternative:
df = df[pd.Series(list(zip(df.A, df.B)), index=df.index).isin(AB_col)]
Or you can compare MultiIndex
created by set_index
:
df = df[df.set_index(['A','B']).index.isin(AB_col)]
Or create your own MultiIndex and filter:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With