benchmarks[(benchmarks["Region"] == "EU") & (benchmarks["Channel"] == "Email") & (benchmarks["Metric"] == "Unique Open Rate")]["Benchmark"][0]
在数据框benchmarks
中,("地区","频道","指标")是列中值的唯一键"基准&#34 ;.因此,这个pandas声明产生一个数字。对于这样一个简单的选择来说,它非常清晰,但也有很多类型 - 是否有简短形式?
答案 0 :(得分:2)
我想是的,如果您使用query
:
q = "Region == 'EU' & Channel == 'Email' & Metric == 'Unique Open Rate'"
benchmarks.query(q)["Benchmark"][0]
样品:
benchmarks = pd.DataFrame({'Region':['EU','USA'],
'Channel':['Email','Text'],
'Metric':['Unique Open Rate','Rate'],
'Benchmark':[5,2]})
print (benchmarks)
Benchmark Channel Metric Region
0 5 Email Unique Open Rate EU
1 2 Text Rate USA
q = "Region == 'EU' & Channel == 'Email' & Metric == 'Unique Open Rate'"
a = benchmarks.query(q)["Benchmark"][0]
print (a)
5