问题
How to simplify or break down the super long IF Condition in Python pandas?
my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
good_dataframes = []
for df_name, df in my_dataframes.items():
if ((df.loc[1:4, 'test'] <= 6).all()
and (df.loc[5:9, 'dif'] < 9).all()) or ((df.loc[1:5, 'test'] <= 6).all()
and (df.loc[5:8, 'dif'] < 8).all()) or ((df.loc[1:8, 'test'] <= 6).all()
and (df.loc[5:8, 'dif'] < 9).all()):
good_dataframes.append(df_name)
the challenge for me is put the right indent
回答1:
you can break down in 3 major check functions:
my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
def check_1(df):
return (df.loc[1:4, 'test'] <= 6).all() and (df.loc[5:9, 'dif'] < 9).all()
def check_2(df):
return (df.loc[1:5, 'test'] <= 6).all() and (df.loc[5:8, 'dif'] < 8).all()
def check_3(df):
return (df.loc[1:8, 'test'] <= 6).all() and (df.loc[5:8, 'dif'] < 9).all()
CHECK_FUNCTIONS = (check_1, check_2, check_3)
def check(df):
return any(check_f(df) for check_f in CHECK_FUNCTIONS)
good_dataframes = []
for df_name, df in my_dataframes.items():
if check(df):
good_dataframes.append(df_name)
to obtain good_dataframes you can use a list comprehension:
good_dataframes = [df_name for df_name, df in my_dataframes.items() if check(df)]
回答2:
Define a function that contains the conditions.
def is_good_df(df):
return (((df.loc[1:4, 'test'] <= 6).all()
and (df.loc[5:9, 'dif'] < 9).all())
or ((df.loc[1:5, 'test'] <= 6).all()
and (df.loc[5:8, 'dif'] < 8).all())
or ((df.loc[1:8, 'test'] <= 6).all()
and (df.loc[5:8, 'dif'] < 9).all()))
A good IDE should help with getting the indentation right.
Notice that you were also missing the ()
after some of your .all
calls.
Then you can do
my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
good_dataframes = [df_name for df, df_name in my_dataframes.items() if is_good_df(df)]
回答3:
would this make it more understandable:
my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
good_dataframes = []
for df_name, df in my_dataframes.items():
if ((df.loc[1:4, 'test'] <= 6).all and (df.loc[5:9, 'dif'] < 9).all()) or
((df.loc[1:5, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 8).all()) or
((df.loc[1:8, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 9).all()):
good_dataframes.append(df_name)
回答4:
You can put your and
expressions in a list and then check if any of the values is True
(matches with your or
logic):
my_dataframes = {'d1': d1, 'd2': d2,'d3': d3}
good_dataframes = []
for df_name, df in my_dataframes.items():
conds = [(df.loc[1:4, 'test'] <= 6).all and (df.loc[5:9, 'dif'] < 9).all(),
(df.loc[1:5, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 8).all(),
(df.loc[1:8, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 9).all()]
if any(conds):
good_dataframes.append(df_name)
回答5:
You can use the logical operation like and, or etc.
`my_dataframes = {'d1': d1, 'd2': d2,'d3': d3} good_dataframes = [] for df_name, df in my_dataframes.items():
conds = [(df.loc[1:4, 'test'] <= 6).all and (df.loc[5:9, 'dif'] < 9).all(),
(df.loc[1:5, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 8).all(),
(df.loc[1:8, 'test'] <= 6).all and (df.loc[5:8, 'dif'] < 9).all()]
if any(conds):
good_dataframes.append(df_name)
`
来源:https://stackoverflow.com/questions/59586706/python-break-down-a-super-long-if-statement