-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented filter on Dataframe for flower data processing. (#485)
- Loading branch information
1 parent
325476c
commit f673763
Showing
4 changed files
with
449 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pandas as pd | ||
|
||
|
||
def apply_filter(df, filter_rule): | ||
condition = filter_rule.get("condition", "AND") | ||
rules = filter_rule.get("rules", []) | ||
|
||
if condition == "AND": | ||
filtered_df = df | ||
for rule in rules: | ||
filtered_df = apply_single_rule(filtered_df, rule) | ||
return filtered_df | ||
elif condition == "OR": | ||
filtered_dfs = [] | ||
for rule in rules: | ||
filtered_dfs.append(apply_single_rule(df, rule)) | ||
return pd.concat(filtered_dfs).drop_duplicates().reset_index(drop=True) | ||
|
||
|
||
def apply_single_rule(df, rule): | ||
if "condition" in rule: | ||
return apply_filter(df, rule) | ||
else: | ||
id = rule["id"] | ||
operator = rule["operator"] | ||
value = rule.get("value", None) | ||
|
||
operator_functions = { | ||
"equal": lambda df, id, value: df[df[id] == value], | ||
"not_equal": lambda df, id, value: df[df[id] != value], | ||
"greater": lambda df, id, value: df[df[id] > value], | ||
"less": lambda df, id, value: df[df[id] < value], | ||
"greater_or_equal": lambda df, id, value: df[df[id] >= value], | ||
"less_or_equal": lambda df, id, value: df[df[id] <= value], | ||
"between": lambda df, id, value: df[df[id].between(value[0], value[1])], | ||
"not_between": lambda df, id, value: df[ | ||
~df[id].between(value[0], value[1]) | ||
], | ||
"is_not_null": lambda df, id, value: df[df[id].notnull()], | ||
"is_null": lambda df, id, value: df[df[id].isnull()], | ||
"in": lambda df, id, value: df[df[id].isin(value)], | ||
} | ||
|
||
if operator in operator_functions: | ||
return operator_functions[operator](df, id, value) | ||
else: | ||
raise ValueError(f"Unsupported operator: {operator}") |
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
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
Oops, something went wrong.