通过一系列索引查找给定数据帧的值?

时间:2017-05-03 09:42:41

标签: python pandas

我正在构建房价预测模型,假设我对北京各区的平均房价有一个DataFrame df。我从训练集中得到它。

>>>df
>>>df
         price
area
Haidian  80448
Xicheng  113698
Chaoyang 66424
...

现在我怎样才能通过pandas Series对象查询价格?让我们说它是

0                         Xicheng
1                         Haidian
2                       Dongcheng
3                        Chaoyang
...

感谢。

1 个答案:

答案 0 :(得分:0)

我认为您需要loc,如有必要,请添加intersection

df = pd.DataFrame({'price':[80448, 113698, 66424]}, index=['Haidian', 'Xicheng', 'Chaoyang'])
df.index.name = 'area'
print (df)
           price
area            
Haidian    80448
Xicheng   113698
Chaoyang   66424

s = pd.Series(['Xicheng','Haidian','Dongcheng','Chaoyang'])
print (s)
0      Xicheng
1      Haidian
2    Dongcheng
3     Chaoyang
dtype: object
print (df.loc[s])
              price
area               
Xicheng    113698.0
Haidian     80448.0
Dongcheng       NaN
Chaoyang    66424.0

print (df.loc[df.index.intersection(s)])
           price
Xicheng   113698
Haidian    80448
Chaoyang   66424