【发布时间】:2019-01-21 17:48:20
【问题描述】:
我有这个房地产数据:
neighborhood type_property type_negotiation price
Smallville house rent 2000
Oakville apartment for sale 100000
King Bay house for sale 250000
...
我创建了一个函数,该函数根据您输入的社区以及是否是待售房屋对这个大型数据集进行排序,然后返回这些房屋的第 10 和第 90 个百分位数和数量。我在下面:
def foo(string):
a = df[(df.type_negotiation == 'forsale')&(df.type_property == 'house')&(df.neighborhood == string)]
b = pd.DataFrame([[a.price.quantile(0.1), a.price.quantile(0.9), len(a.index)]],
columns=('tenthpercentile', 'ninetiethpercentile', 'Quantity'))
return b
print(foo('KingBay'))
tenthpercentile ninetiethpercentile Quantity
0 250000.0 250000.0 1
我想编写一个循环来为我拥有的社区列表执行此操作,然后将每个返回编译到一个新的数据帧中。看起来像这样:
tenthpercentile ninetiethpercentile Quantity
King Bay 250000.0 250000.0 1
Smallville 99000.0 120000.0 8
Oakville 45000.0 160000.0 6
提前谢谢你。
【问题讨论】:
标签: python pandas function loops dataframe