Created
October 7, 2020 11:29
-
-
Save titaneric/c33bb674a1305f5d9194a4b2cd52ad68 to your computer and use it in GitHub Desktop.
Solution for @loser113 at PTT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | B | C | D | |
---|---|---|---|---|
1 | 2 | 3 | 4 | |
2 | 3 | 4 | 5 | |
5 | 6 | 7 | 8 | |
2 | 3 | 4 | 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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