Python 熊猫应用功能速度慢

Python 熊猫应用功能速度慢,python,pandas,apply,Python,Pandas,Apply,我有一个数据帧(大约1-3米记录),我正在其上运行apply()函数。这需要相当长的时间。我读过一些不应该使用apply()的地方,但是我不确定如何在没有它的情况下完成同样的任务 数据框架是事务性销售数据。我按“APN”分组,然后重新创建一个新的pd系列 def f(x): d = {} d['fips_2'] = x["fips"].values[0] d['apn_2'] = x["apn"].values[0] d['most_recent_sale'] =

我有一个数据帧(大约1-3米记录),我正在其上运行apply()函数。这需要相当长的时间。我读过一些不应该使用apply()的地方,但是我不确定如何在没有它的情况下完成同样的任务

数据框架是事务性销售数据。我按“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)

有没有更好的方法可以更快地完成相同的任务?

一个改进是删除几个最大的调用,并在开始时进行一次排序。我不知道示例数据集中的所有列都丢失了,但类似的方法可能会起作用:

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']

这是一个数据帧。过去年份表示过去一年内的交易。然后,我按特定项目“apn”分组,并需要查看每个“apn”的情况,例如最近的销售额、第二个最近的销售价格等。