7

pandas 怎么对比当前行的前面 7 天的所有某列的数字?

 2 years ago
source link: https://www.v2ex.com/t/814540
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

pandas 怎么对比当前行的前面 7 天的所有某列的数字?

  keroppi · 8 小时 43 分钟前 · 208 次点击
商场每天销售额都有记录(按天统计),其中有一列是总销售额(total),现需求是:用总销售额计算每一天与过去 N 天(例如一周 7 天)进行对比,找出两个结果:
1 当天是不是过去一周( 7 )销售额最低的
2 如果不是,过去一周哪天最低的销售额是多少?


我尝试过用遍历 range(1, 8),然后 shift(1), shift(2) 这样总感觉不太科学,不知道有各位大佬有更加方便的办法?
2 条回复    2021-11-11 00:38:01 +08:00

keroppi

keroppi   8 小时 38 分钟前

我目前自己写的代码(第 2 个要求还没搞)
```
for i in range(1, end_num):

df['prev_row'] = df['total'].shift(i)

df['range_compare_result'] = df['total'].lt(df['prev_row'])

del df['prev_row']
# end for
```

necomancer

necomancer   7 小时 34 分钟前

In [2]: ret = []

In [2]: for i in range(7, a.shape[0]+1):
...: m = np.argmin(a[i-7:i]) +i -7
...: ret.append((m, a[m], m==i-1))
...:

In [3]: ret
Out[3]:
[(4, 0.1070058697941636, False), # (绝对索引,值,当日是否为当周(前 7 日)最低)
(4, 0.1070058697941636, False),
(4, 0.1070058697941636, False),
(4, 0.1070058697941636, False),
(4, 0.1070058697941636, False),
(7, 0.38082268305528855, False),
(7, 0.38082268305528855, False),
(13, 0.3198102115371413, True),
(13, 0.3198102115371413, False),
(15, 0.26007158139013975, True),
(15, 0.26007158139013975, False),
(15, 0.26007158139013975, False),
(18, 0.1774755070886418, True),
(18, 0.1774755070886418, False)]

In [4]: a
Out[4]:
array([0.59171944, 0.95287085, 0.56036765, 0.91771266, 0.10700587,
0.67920182, 0.40034268, 0.38082268, 0.81140219, 0.78271362,
0.43178875, 0.7328393 , 0.93324926, 0.31981021, 0.74938937,
0.26007158, 0.33768583, 0.78881252, 0.17747551, 0.27862649])

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK