1 回答
![?](http://img1.sycdn.imooc.com/533e50ed0001cc5b02000200-100-100.jpg)
TA贡献1864条经验 获得超6个赞
通常使用数据框,如果可以的话,最好避免显式循环,并使用提供的优化方法pandas。在您的情况下,可以通过使用groupbywith来消除循环describe,将所需的百分位数传递给parameter percentiles。然后,只需选择所需的列并适当地重命名它们即可:
new_df = (df.groupby('neighborhood')
.describe(percentiles=[0.1,0.9])
['price'][['10%','90%','count']]
.rename(columns={'count':'Quantity',
'10%':'tenthpercentile',
'90%':'ninetiethpercentile'}))
在您的情况下(因为每个邻域只有一个示例):
>>> new_df
tenthpercentile ninetiethpercentile Quantity
neighborhood
King Bay 250000.0 250000.0 1.0
Oakville 100000.0 100000.0 1.0
Smallville 2000.0 2000.0 1.0
[编辑]:我只是在您的函数中看到您只是在看(df.type_negotiation == 'for sale') & (df.type_property == 'house')。为此,只需添加aloc即可通过以下条件过滤数据框:
new_df = (df.loc[(df.type_negotiation == 'for sale')
& (df.type_property == 'house')]
.groupby('neighborhood')
.describe(percentiles=[0.1,0.9])
['price'][['10%','90%','count']]
.rename(columns={'count':'Quantity',
'10%':'tenthpercentile',
'90%':'ninetiethpercentile'}))
另外,如果您热衷于使用函数和循环(不是我建议的话),则可以执行以下操作:
pd.concat([foo(i) for i in df.neighborhood.unique()])
添加回答
举报