如何使用另一个pandas系列返回pandas数据框中的索引列表?

时间:2018-06-08 12:57:26

标签: python pandas pandas-groupby

我正在使用kaggle house price dataset,我有以下代码来计算四分位数范围

# bin by area
df['sqft_area_binned']=pd.cut(x=df['sqft_living'], bins=5)
q1 = df.groupby(['sqft_area_binned'])['price'].quantile(0.25)
q3 = df.groupby(['sqft_area_binned'])['price'].quantile(0.75)
iqr = q3 - q1
upper = q3 + 1.5*iqr
lower = q1 - 1.5*iqr
print(upper)
>>>
sqft_area_binned
(276.75, 2940.0]        946000.0
(2940.0, 5590.0]       1900000.0
(5590.0, 8240.0]       4332500.0
(8240.0, 10890.0]     10210500.0
(10890.0, 13540.0]    10410000.0
Name: price, dtype: float64

现在,我想返回一个id列表(df中的第一列)sqft_area_binned 对应的lower上面相应的upper

例如,如果一所房子(df中的一行)有sqft_area_binned=(276.75, 2940.0]price> 946000.0,然后返回id

这怎么可能,可能使用过滤或.isin()

1 个答案:

答案 0 :(得分:1)

query = df.index[(df.sqft_area_binned == desiredBin) & (df.price > upperPriceBound)]