开发者社区> 问答> 正文

熊猫应用功能速度慢

我有一个dataframe(大约1 - 3 M条记录),我在上面运行一个apply()函数。这要花相当多的时间。我读过一些不应该使用apply()的地方,但是我不知道如何在没有它的情况下完成相同的任务。 dataframe是事务性销售数据。我按“APN”分组,然后重新创建一个新的pd.系列

def f(x):
    d = {}
    d['fips_2'] = x["fips"].values[0]
    d['apn_2'] = x["apn"].values[0]
    d['most_recent_sale'] = x["recording_date"].nlargest(1).iloc[-1]
    d['second_most_recent_sale'] = x["recording_date"].nlargest(2).iloc[-1]
    d['third_most_recent_sale'] = x["recording_date"].nlargest(3).iloc[-1]
    d['most_recent_price'] = x.loc[x["recording_date"] == d["most_recent_sale"], "price"].values[0]
    d['second_most_recent_price'] = x.loc[x["recording_date"] == d["second_most_recent_sale"], "price"].values[0]
    d['third_most_recent_price'] = x.loc[x["recording_date"] == d["third_most_recent_sale"], "price"].values[0]
    d['second_grantor'] = x.loc[x["recording_date"] == d["most_recent_sale"], "seller"].values[0]
    d['prior_grantor'] = x.loc[x["recording_date"] == d["second_most_recent_sale"], "seller"].values[0]
    d['type'] = x["type"].values[0]

    print(x["apn"].values[0])

    return pd.Series(d, index=['apn_2', 'most_recent_sale', 'second_most_recent_sale', 'most_recent_price', 'second_most_recent_price', 'second_grantor', 'type'])

df_grouped = year_past_df.groupby("apn").apply(f)

有没有更好的方法可以更快地完成同样的任务? 问题来源StackOverflow 地址:/questions/59384606/pandas-apply-function-slow-speed

展开
收起
kun坤 2019-12-26 14:34:23 407 0
1 条回答
写回答
取消 提交回答
  • 一个改进是删除几个最大的调用并在开始时进行一次排序。我不知道所有的列作为一个示例数据集是失踪,但像这样的东西可能工作:

    def f(x):
        x = x.sort_values("recording_date")
        d = {}
        d['fips_2'] = x["fips"].values[0]
        d['apn_2'] = x["apn"].values[0]
        d['most_recent_sale'] = x.sale.iloc[-1]
        d['second_most_recent_sale'] = x.sale.iloc(-2)
        d['third_most_recent_sale'] = x.sale.iloc(-2)
        d['most_recent_price'] = x.price.iloc(-1)
        d['second_most_recent_price'] = x.price.iloc(-2)
        d['third_most_recent_price'] = x.price.iloc(-3)
        d['second_grantor'] = x.seller.iloc(-1)
        d['prior_grantor'] = x.seller.iloc(-2)
        d['type'] = x["type"].values[0]
        return pd.Series(d, index=['apn_2', 'most_recent_sale', 'second_most_recent_sale', 'most_recent_price', 'second_most_recent_price', 'second_grantor', 'type'])
    
    df_grouped = year_past_df.groupby("apn").apply(f)
    

    另一种选择是在开始时对整个数据集进行排序,然后使用类似这样的agg函数:

    agg_dir = {
        'fips': 'first',
        'sale': ['last', lambda x: x.iloc[-2], lambda x: x.iloc[-3]],
        'price': ['last', lambda x: x.iloc[-2], lambda x: x.iloc[-3]],
        'seller': ['last', lambda x: x.iloc[-2]],
        'type': 'first'
    }
    df_grouped = year_past_df.sort_values("recording_date").groupby("apn").agg(agg_dir)
    df_grouped.columns = ['fips_2', 'most_recent_sale', 'second_most_recent_sale', 
                          'third_most_recent_sale', 'most_recent_price', 'second_most_recent_price', 
                          'third_most_recent_price', 'second_grantor', 'prior_grantor', 'type']
    
    2019-12-26 14:34:35
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
微信广告引擎与播放节奏算法实践 立即下载
让世界没有陌生的角落共享单车时代的快与慢 立即下载
让世界没有陌生的角落 共享单车时代的快与慢 立即下载