首页 > 解决方案 > 在 Python / Pandas 中为每次具有多条记录的时间序列数据添加新的“步骤”值列

问题描述

我正在尝试分配一个新的 df 列“step”,其中每一行的df['step']值对于不同列中的每个唯一值(“时间”)递增。时间列按升序排列,tag_id 的顺序并不重要。每个唯一时间戳可能有不同数量的唯一 tag_id 值,但所有时间值都是规则间隔的,相隔 00:00:00:05。

数据集看起来像这样,带有时间戳,并且每次都有多个具有 x 和 y 位置的唯一 tag_id。

    tag_id      x_pos      y_pos             time  
0        1  77.134000  70.651000         19:03:51 
1        2  66.376432  34.829683         19:03:51     
2        3  49.250835  37.848381         19:03:51     
3        1  50.108018   7.670564  19:03:51.050000     
4        2  54.919299  47.613906  19:03:51.050000     
5        3  57.584265  38.440233  19:03:51.050000     
6        1  47.862124  29.133489  19:03:51.100000     
7        2  71.092900  71.650500  19:03:51.100000     
8        3  65.704667  25.856978  19:03:51.100000     
9        1  62.680708  13.710716  19:03:51.150000     
10       2  65.673670  47.574349  19:03:51.150000     
11       3  77.134000  70.651000  19:03:51.150000     
12       1  66.410406  34.792751  19:03:51.200000     
13       2  49.306861  37.714626  19:03:51.200000     
14       3  50.142578   7.575307  19:03:51.200000     
15       1  54.940298  47.528109  19:03:51.250000     

我为 中的每个唯一值使用掩码创建了以下函数,该函数df['time']有效但速度极慢(原始数据集约 500,000 条记录,41,000 次唯一时间)。

# after adding step column by:
# df['step'] = 0

def timeToSteps(df):
    count = 0
    for t in df['time'].unique():
        mask = df['time'].values == t
        df.loc[mask, ['step']] = count
        count += 1

给予:

    tag_id      x_pos      y_pos             time  step  
0        1  77.134000  70.651000         19:03:51     0
1        2  66.376432  34.829683         19:03:51     0
2        3  49.250835  37.848381         19:03:51     0
3        1  50.108018   7.670564  19:03:51.050000     1
4        2  54.919299  47.613906  19:03:51.050000     1
5        3  57.584265  38.440233  19:03:51.050000     1
6        1  47.862124  29.133489  19:03:51.100000     2
7        2  71.092900  71.650500  19:03:51.100000     2
8        3  65.704667  25.856978  19:03:51.100000     2
9        1  62.680708  13.710716  19:03:51.150000     3
10       2  65.673670  47.574349  19:03:51.150000     3
11       3  77.134000  70.651000  19:03:51.150000     3
12       1  66.410406  34.792751  19:03:51.200000     4
13       2  49.306861  37.714626  19:03:51.200000     4
14       3  50.142578   7.575307  19:03:51.200000     4
15       1  54.940298  47.528109  19:03:51.250000     5

有没有更有效的方法来实现这个结果?谢谢!

标签: pythonpython-3.xpandasdataframetime-series

解决方案


尝试这个

import numpy as np
import pandas as pd

df = pd.read_csv('data.txt', delim_whitespace=True, parse_dates=['time'])
df['step'] = df['time']-df['time'].shift(1)     #shift index and find difference
zero = np.timedelta64(0, 's')       
df['step'][0] = np.timedelta64(0, 's')          #change first var from naT to zero
df['step'] = df['step'].apply(lambda x: x>zero).cumsum()
print(df)

生产

    tag_id      x_pos      y_pos                    time  step
0        1  77.134000  70.651000 2020-02-16 19:03:51.000     0
1        2  66.376432  34.829683 2020-02-16 19:03:51.000     0
2        3  49.250835  37.848381 2020-02-16 19:03:51.000     0
3        1  50.108018   7.670564 2020-02-16 19:03:51.050     1
4        2  54.919299  47.613906 2020-02-16 19:03:51.050     1
5        3  57.584265  38.440233 2020-02-16 19:03:51.050     1
6        1  47.862124  29.133489 2020-02-16 19:03:51.100     2
7        2  71.092900  71.650500 2020-02-16 19:03:51.100     2
8        3  65.704667  25.856978 2020-02-16 19:03:51.100     2
9        1  62.680708  13.710716 2020-02-16 19:03:51.150     3
10       2  65.673670  47.574349 2020-02-16 19:03:51.150     3
11       3  77.134000  70.651000 2020-02-16 19:03:51.150     3
12       1  66.410406  34.792751 2020-02-16 19:03:51.200     4
13       2  49.306861  37.714626 2020-02-16 19:03:51.200     4
14       3  50.142578   7.575307 2020-02-16 19:03:51.200     4
15       1  54.940298  47.528109 2020-02-16 19:03:51.250     5

推荐阅读