Philipp
Philipp

Reputation: 62

Pandas dataframe - select elements based on list of values for multiple columns

I have a dataframe that looks as follows:

df = pd.DataFrame({
    'C1': ['A','A','A','B', 'B', 'C', 'C'],
    'C2': [1, 2, 3, 1, 2, 1, 2],
    'C3': [2., 3.1, 1.2, 1.4, 2.1, .4, .5]
})

I would like to filter the dataframe for individual combinations on C1 and C2.

For example, I would like to have the combination for the following matches: (C1 == 'A' & C2 == 2) and (C1 == 'B' & C2 == 1).

The resulting data frame should look as follows:

   C1   C2  C3
0   A   2   3.1
1   B   1   1.4

So, I would like to filter on the unique combinations, e.g. given by a list of tuples. For the above example it would look like this:

[('A', 2), ('B', 1)]

Can you guide me how I can accomplish this ideally not using a loop?

Thank you.

Philipp

Upvotes: 1

Views: 714

Answers (2)

Rinshan Kolayil
Rinshan Kolayil

Reputation: 1139

df = pd.DataFrame({
    'C1': ['A','A','A','B', 'B', 'C', 'C'],
    'C2': [1, 2, 3, 1, 2, 1, 2],
    'C3': [2., 3.1, 1.2, 1.4, 2.1, .4, .5]
})
df[df[["C1","C2"]].apply(tuple, 1).isin([('A', 2), ('B', 1)])]
C1 C2 C3
1 A 2 3.1
3 B 1 1.4

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 71689

Let us try MultiIndex.isin to create a boolean mask which can be used to filter the rows

df[df.set_index(['C1', 'C2']).index.isin(l)]

Alternative approach with merge

df.merge(pd.DataFrame(l, columns=['C1', 'C2']))

  C1  C2   C3
1  A   2  3.1
3  B   1  1.4

Upvotes: 1

Related Questions