Reputation: 147
I have 2 dictionary consist of limit values for each category of my Data Frame. These Values will not be fixed and constantly change as time goes by (monthly frequency). So my question here is, how to I filter my data frame so that each category in column ("set") will be filter according to the dictionary values.
Here is the dictionary
lower boundary
{'a': -69.79064324769809,
'b': -4.1828456049705,
'c': -73.1922251093512
}
upper boundary
{'a': 10.79,
'b': 20.18,
'c': 10.19
}
Here is the example of the data frame
set values
a 12
b 1
c 3
a 31
c 2
b 41
Here is the code that I made, but it was not dynamic and need to be manually check and replace.
df_1=df[(["set"]=="a") & (df["values"] < 10.79) & (df["values"] > -69.79064324769809)]
df_2=df[(["set"]=="b") & (df["values"] < 20.18) & (df["values"] > -4.1828456049705)]
df_3=df[(["set"]=="c") & (df["values"] < 10.19) & (df["values"] > -73.1922251093512)]
df_total=pd.concat([df_1,df_2,df_3])"
Upvotes: 0
Views: 226
Reputation: 862691
Use Series.map
for replace set
s column by thresholds:
lower ={'a': -69.79064324769809,
'b': -4.1828456049705,
'c': -73.1922251093512
}
upper ={'a': 10.79,
'b': 20.18,
'c': 10.19
}
df_total=df[(df["values"] < df["set"].map(upper)) & (df["values"] > df["set"].map(lower))]
print (df_total)
set values
1 b 1
2 c 3
4 c 2
Or use Series.between
:
df_total=df[df["values"].between(df["set"].map(lower),df["set"].map(upper),inclusive=False)]
print (df_total)
set values
1 b 1
2 c 3
4 c 2
Upvotes: 2