Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter pandas dataframe from tuples

Tags:

python

pandas

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
df

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

dfexpected

like image 345
Yogi Avatar asked Jan 12 '18 14:01

Yogi


1 Answers

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
like image 142
jezrael Avatar answered Oct 06 '22 23:10

jezrael