我想通过JSON接收的值来过滤数据框,如下所示:
region = request_json['region_value']
ma = request_json['ma_value']
market = request_json['market_value']
subsegment = request_json['subsegment_value']
filtered =df[
(df['Region'] == region) &
(df['MA/Segment'] == ma) &
(df['Market'] == market) &
(df['Subsegment'] == subsegment)
]
但是,如果region
,ma
,market
或subsegment
等于'All'
,我不想在过滤中包括它。
例如,如果region
= 'All'
,则代码应如下所示:
filtered =df[
(df['MA/Segment'] == ma) &
(df['Market'] == market) &
(df['Subsegment'] == subsegment)
]
我想避免使用多个if,而是寻求更优雅的解决方案。我欢迎任何想法。
答案 0 :(得分:1)
您可以使用np.logical_and.reduce
;即使您的列名包含空格,特殊字符等,此方法也将起作用:
conditions = {
'Region': region, 'MA/Segment': ma, 'Market': market, 'Subsegment': subsegment}
mask = pd.np.logical_and.reduce([
df[k] == v for k, v in conditions.items() if v != 'All'])
df[mask]
另一个选项是query
,但这将在您的列名称符合有效的python标识符名称的情况下起作用。
query = ' and '.join([
f'{k}=={repr(v)}' for k, v in conditions.items() if v != 'All'])
df.query(query)
答案 1 :(得分:0)
使用try:
import thatpackage
except Exception as e:
print("importing thatpackage failed", e)
sum