我正在基于基本df和过滤函数创建多个过滤的数据帧,这些函数返回要选择的索引列表。从某种意义上说,它应该可以与多种过滤功能一起使用,并希望得到一些建议。
伪Python:
import pandas as pd
def some_filter_fn(df_index: pd.Index) -> list:
return [list of indices based on some simple condition]
def another_filter_fn(df_index: pd.Index, another_param: str) -> list:
return [list of indices depending on another_param]
def filtered_df(base_df: pd.DataFrame, filter_func) -> pd.DataFrame:
filtered_df = base_df.loc[ base_df.index.isin(filter_func(base_df.index)) ].copy()
# some ops on filtered_df follows
# ...
return filtered_df
我希望能够做到
base_df = pd.DataFrame() # some DF
df1 = filtered_df(base_df, some_filter_fn)
df2 = filtered_df(base_df, another_filter_fn(another_param = 'val'))
理想情况下,以后可以添加任意过滤功能。