我有一個有購買歷史的資料框(最后一個資料框)
我必須生成他們在他們到來的第一個日期、第二個日期、1 周、1 個月等的購買摘要,如下所示:
| 購買者 | 第一天 | 第二天 | 第一周 | 第一個月 | 6個月 | 一年 |
|---|---|---|---|---|---|---|
| 0 | 阿尼爾 | 1 | 0 | 0 | 0 | 1 |
| 1 | 穆克什 | 1 | 0 | 1 | 7 | 0 |
| 2 | 拉維 | 8 | 0 | 0 | 4 | 1 |
我所做的如下:創建了一個摘要
summary=df.groupby('purchaser').agg('min').rename(columns={'date':'min'}).reset_index()
summary['oneday_date']=summary['min'] dt.timedelta(days=1)
summary['oneweek_date']=summary['min'] dt.timedelta(days=7)
summary['onemonth_date']=summary['min'] dt.timedelta(days=30)
summary['sixmonth_date']=summary['min'] dt.timedelta(days=183)
summary['year_date']=summary['min'] dt.timedelta(days=365)
然后對每個購買者進行迭代和計數。
%%time
result=[]
for num, row in summary.iterrows():
purchaser=row['purchaser']
mindate=row['min']
oneday=row['oneday_date']
oneweek=row['oneweek_date']
onemonth=row['onemonth_date']
sixmonth=row['sixmonth_date']
oneyear=row['year_date']
subdf=df[df['purchaser']==purchaser]
count0=len(subdf[(subdf['date']>=mindate) & (subdf['date']<oneday)])
count1=len(subdf[(subdf['date']>=oneday) & (subdf['date']<oneweek)])
count2=len(subdf[(subdf['date']>=oneweek) & (subdf['date']<onemonth)])
count3=len(subdf[(subdf['date']>=onemonth) & (subdf['date']<sixmonth)])
count4=len(subdf[(subdf['date']>=sixmonth) & (subdf['date']<oneyear)])
count5=len(subdf[subdf['date']>=oneyear])
result.append([purchaser,count0,count1,count2,count3,count4,count5])
CPU times: user 13.2 ms, sys: 587 μs, total: 13.8 ms
Wall time: 11.9 ms
我的實際資料比這大 1000,000 倍。
我已經嘗試過的是
- 按日期索引資料框
df=df.set_index('date') - 排序
subdf上dates
兩者都沒有帶來任何速度提升
完整資料
df=pd.DataFrame({'purchaser':['anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'anil', 'mukesh', 'mukesh', 'mukesh', 'mukesh', 'mukesh', 'mukesh', 'mukesh', 'mukesh', 'mukesh', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi', 'ravi'],
'article':['pencil', 'pencil', 'pencil', 'pencil', 'rubber', 'rubber', 'rubber', 'rubber', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'pencil', 'pencil', 'rubber', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'pencil', 'pencil', 'pencil', 'pencil', 'pencil', 'pencil', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'sharpner', 'rubber'],
'date':[1611316328000000000, 1612432758000000000, 1616319170000000000, 1622455063000000000, 1604242496000000000, 1604245635000000000, 1605421133000000000, 1570823168000000000, 1594919491000000000, 1604248351000000000, 1604237937000000000, 1604233396000000000, 1604251740000000000, 1601216201000000000, 1604232509000000000, 1604249925000000000, 1604246581000000000, 1603559931000000000, 1603946050000000000, 1603956529000000000, 1604228447000000000, 1604233557000000000, 1604212924000000000, 1604212924000000000, 1604212924000000000, 1612539904000000000, 1614939815000000000, 1614964750000000000, 1621581174000000000, 1604218928000000000, 1604222345000000000, 1604239015000000000, 1613635361000000000, 1604208994000000000]})
df['date']=pd.to_datetime(df['date'])
uj5u.com熱心網友回復:
演算法:
- 使用 計算每個購買者的第一個購買日期
.groupby(),然后計算第二個日期、第一周、...一年的日期。將其保存在第二個資料框中。 - 將這些日期左連接到包含“購買者”列中所有購買的原始資料集。
- 根據全購集 df 中的這些日期列計算所需的列。這現在可以使用向量化操作來完成,而不是遍歷整個陣列,從而加快運行時間。
.groupby()在購買者上并匯總每列的計數以產生最終所需的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/404900.html
標籤:
