1

I would like to be able to filter pandas using 2 lists. One list contains columns to be filtered, the second list contains which values of columns on the first list shall be selected respectively.

I have prepared an example:

import seaborn as sns
import pandas as pd

dataf = sns.load_dataset('tips')

cols = ['tip', 'sex']
vals = [1.01, 'Female']

cols2 = ['tip', 'smoker', 'day']
vals2 = [3.00, 'No', 'Sun']

# Pseudo idea of what I need
# Pseudo idea of what I need
dataf.loc[lambda d: d[cols] == vals]
# should be equal to
dataf.loc[(dataf['sex'] == 'Female') & (dataf['tip'] == 1.01)]


dataf.loc[lambda d: d[cols2] == vals2]
# should be equal to
dataf.loc[(dataf['smoker'] == 'No') & (dataf['tip'] == 3) & (dataf['day'] == 'Sun')]

I gave also an idea of what I need. However, it is very important that this should be generalizable, meaning that cols and cols2 can have a different numbers of elements within.

It will always hold that len(cols) == len(vals) ...

3
  • provide some sample data... an approach would be build a string that is valid to query() Commented Mar 16, 2021 at 20:31
  • What in case only one of the columns holds the desired value, but the other does not, e.g., [2, 'Male']? Commented Mar 16, 2021 at 20:42
  • Shall return it only, when all conditions are met. Commented Mar 16, 2021 at 20:43

1 Answer 1

1

This code will provide only the lines which has all the values as in the desired values array:

dataf.loc[:, cols][np.product(np.int8(dataf.loc[:, cols].isin(vals)), axis=1, dtype=bool)]

Output:


tip sex
0   1.01    Female

Cheers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.