-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerying_work.py
53 lines (33 loc) · 1.26 KB
/
querying_work.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import pandas as pd
import datetime
from pip._internal.utils.misc import tabulate
def filter_by_range(df, column_name, lower_bound, upper_bound):
"""Filters a DataFrame based on a column's value range.
Args:
df: The input DataFrame.
column_name: The name of the column to filter.
lower_bound: The lower bound of the range.
upper_bound: The upper bound of the range.
Returns:
A new DataFrame containing rows that meet the filtering criteria.
"""
return df[(df[column_name] >= lower_bound) & (df[column_name] <= upper_bound)]
def filter_by_text(df, column_name, text):
"""Filters a DataFrame based on a column's text content.
Args:
df: The input DataFrame.
column_name: The name of the column to filter.
text: The text to search for.
Returns:
A new DataFrame containing rows that match the text.
"""
return df[df[column_name].str.contains(text, case=False)]
def save_filtered_df(filtered_df):
"""Saves the filtered DataFrame to a new CSV file with a timestamp.
Args:
filtered_df: The filtered DataFrame.
"""
timestamp = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
filename = f"queryAt_{timestamp}.csv"
filtered_df.to_csv(filename, index=False)
print(f"Filtered DataFrame saved to {filename}")