Skip to content

Instantly share code, notes, and snippets.

@titaneric
Created October 7, 2020 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titaneric/c33bb674a1305f5d9194a4b2cd52ad68 to your computer and use it in GitHub Desktop.
Save titaneric/c33bb674a1305f5d9194a4b2cd52ad68 to your computer and use it in GitHub Desktop.
Solution for @loser113 at PTT
A B C D
1 2 3 4
2 3 4 5
5 6 7 8
2 3 4 6
import numpy as np
import pandas as pd
df = pd.read_csv("data.csv")
# find rows that "A" column is 1
print(df[df["A"] == 1])
# find rows that "A" column is in [1, 2]
q = [1, 2]
print(df[df["A"].isin(q)])
# more flexible filter
keys = ["A", "B"]
values = [1, 3]
condition = np.array([
(df[k] == v).values for k, v in zip(keys, values)
])
print(condition)
mask = np.logical_or.reduce(condition)
print(mask)
print(df[mask])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment