我有熊猫数据框和条件列表:
import pandas as pd
inp = [{'c1':10, 'c2':100, 'c3':120}, {'c1':11,'c2':110,'c3':120}, {'c1':12,'c2':120, 'c3':25}]
df = pd.DataFrame(inp)
filter1 = df.c1>10
filter2 = df.c2>100
filter3 = df.c3>20
然后我比较行同时适用于所有条件的条件,然后取这些行
filter_df=np.diff(((filter1)&(filter2)&(filter3)))
df[np.insert(filter_df, 0, False)]
我的代码可以工作,但是如何创建一个可以传递条件列表(可能带有for或while循环)并返回df的方法?如下所示:
list_of_condition = [filter1, filter2, filter3]
def get_true(df, list_of_condition):
filter_df=np.diff(((filter1)&(filter2)&(filter3)))
df = df[np.insert(filter_df, 0, False)]
return df
time = get_true(df, list_of_condition)
在这里我分离地插入过滤器,但是我只想通过df和条件列表。并使用条件列表和for循环检查数据框。
谢谢
答案 0 :(得分:1)
在您的函数中,您可以先计算总体过滤器
def get_true(df, list_of_condition):
# calculate overall filter here
all_filters = list_of_condition[0]
for cond in list_of_condition[1:]:
all_filters &= cond
# do your thing
filter_df=np.diff(all_filters)
df = df[np.insert(filter_df, 0, False)]
return df