我有个数据:
Date Price
2021-01-01 29344.67
2021-01-02 32072.08
2021-01-03 33048.03
2021-01-04 32084.61
2021-01-05 34105.46
2021-01-06 36910.18
2021-01-07 39505.51
2021-01-08 40809.93
2021-01-09 40397.52
2021-01-10 38505.49
Date object
Price float64
dtype: object我的目标是找到最长的连续增长时期。它应该返回:Longest consecutive period was from 2021-01-04 to 2021-01-08 with increase of $8725.32,老实说,我不知道从哪里开始。这是我对熊猫的第一步,我不知道我应该用哪些工具来获取这些信息。
有人能帮我/指出正确的方向吗?
发布于 2021-04-05 15:16:19
在递减上用累积量检测你的递增序列
df['is_increasing'] = df['Price'].diff().lt(0).cumsum()你会得到:
Date Price is_increasing
0 2021-01-01 29344.67 0
1 2021-01-02 32072.08 0
2 2021-01-03 33048.03 0
3 2021-01-04 32084.61 1
4 2021-01-05 34105.46 1
5 2021-01-06 36910.18 1
6 2021-01-07 39505.51 1
7 2021-01-08 40809.93 1
8 2021-01-09 40397.52 2
9 2021-01-10 38505.49 3现在,您可以用
sizes=df.groupby('is_increasing')['Price'].transform('size')
df[sizes == sizes.max()]你得到的是:
Date Price is_increasing
3 2021-01-04 32084.61 1
4 2021-01-05 34105.46 1
5 2021-01-06 36910.18 1
6 2021-01-07 39505.51 1
7 2021-01-08 40809.93 1发布于 2021-04-05 15:39:00
就像Quang为分割组所做的事情,然后选择组数。
s = df.Price.diff().lt(0).cumsum()
out = df.loc[s==s.value_counts().sort_values().index[-1]]
Out[514]:
Date Price
3 2021-01-04 32084.61
4 2021-01-05 34105.46
5 2021-01-06 36910.18
6 2021-01-07 39505.51
7 2021-01-08 40809.93https://stackoverflow.com/questions/66955196
复制相似问题