gpt4 book ai didi

python - pandas 列中的连续值

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:53 25 4
gpt4 key购买 nike

我有 Pandas df['realize']

time                      realize
2016-01-18 08:25:00 -46.369083
2016-01-19 14:30:00 -819.010738
2016-01-20 11:10:00 -424.955847
2016-01-21 07:15:00 27.523859
2016-01-21 16:10:00 898.522762
2016-01-25 00:00:00 761.063545

在哪里time是:

df.index = df['time']
df.index = pd.to_datetime(df.index)

在哪里df['realize']是:

In: type(df['realize'])
Out: pandas.core.series.Series

我想计算连续的值,规则很简单(df['realize'] > 0, df['realize'] < 0)

预期:

time                      realize    Consecutive
2016-01-18 08:25:00 -46.369083 1
2016-01-19 14:30:00 -819.010738 2
2016-01-20 11:10:00 -424.955847 3
2016-01-21 07:15:00 27.523859 1
2016-01-21 16:10:00 898.522762 2
2016-01-25 00:00:00 761.063545 3

我阅读了有关循环的主题,但没有找到我需要的内容。在此先感谢您的帮助。

最佳答案

您可以执行以下操作:

g = df.realize.gt(0).astype(int).diff().fillna(0).abs().cumsum()
df['Consecutive'] = df.groupby(g).realize.cumcount().add(1)

time realize Consecutive
0 2016-01-18 08:25:00 -46.369083 1
1 2016-01-19 14:30:00 -819.010738 2
2 2016-01-20 11:10:00 -424.955847 3
3 2016-01-21 07:15:00 27.523859 1
4 2016-01-21 16:10:00 898.522762 2
5 2016-01-25 00:00:00 761.063545 3

其中使用的石斑鱼是通过取 bool 系列的第一个差值 (DataFrame.diff) 获得的,指示 realize 是否大于 0:

diff = df.realize.gt(0).astype(int).diff().fillna(0).abs()
df.assign(diff = diff, grouper = g)

time realize Consecutive diff grouper
0 2016-01-18 08:25:00 -46.369083 1 0.0 0.0
1 2016-01-19 14:30:00 -819.010738 2 0.0 0.0
2 2016-01-20 11:10:00 -424.955847 3 0.0 0.0
3 2016-01-21 07:15:00 27.523859 1 1.0 1.0
4 2016-01-21 16:10:00 898.522762 2 0.0 1.0
5 2016-01-25 00:00:00 761.063545 3 0.0 1.0

关于python - pandas 列中的连续值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55008331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com