Suppose we have a master dictionary master_dict = {"a": df1, "b": df2, "c": df3}
. Now suppose we have a list called condition_list
. Suppose func
is a function that returns a new dictionary that has the original keys of master_dict
along with potentially new keys.
What is the best way to get the below code to work when the length of condition_list
is greater than 2:
JavaScript
x
6
1
if(len(condition_list) == 1):
2
df = master_dict[condition_list[0]]
3
else:
4
df = func(master_dict(condition_list[0]))
5
df = df[condition_list[1]]
6
Advertisement
Answer
You need to ask clearly. Declare input and output. And try to make a demo code. Anyway, use a loop.
JavaScript
1
4
1
for i in range(len(condition_list)):
2
if i==0: df = master_dict[condition_list[i]]
3
else: df = func(df)[condition_list[i]];
4
If the “df” is a dataframe of pandas, the conditions can be applied at once. Search “select dataframe with multiple conditions”