Pandas为每个时间段分配组号

2024-09-27 00:13:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据框,如下所示

Key     Name    Val1    Val2    Timestamp
101     A       10      1       01-10-2019 00:20:21
102     A       12      2       01-10-2019 00:20:21
103     B       10      1       01-10-2019 00:20:26
104     C       20      2       01-10-2019 14:40:45
105     B       21      3       02-10-2019 09:04:06
106     D       24      3       02-10-2019 09:04:12
107     A       24      3       02-10-2019 09:04:14
108     E       32      2       02-10-2019 09:04:20
109     A       10      1       02-10-2019 09:04:22
110     B       10      1       02-10-2019 10:40:49

从最早的时间戳开始,即“01-10-2019 00:20:21”,我需要创建每个10秒的时间槽,并为时间槽中具有时间戳的所有行分配相同的组号。 输出应如下所示

Key     Name    Val1    Val2    Timestamp               Group
101     A       10      1       01-10-2019 00:20:21     1
102     A       12      2       01-10-2019 00:20:21     1
103     B       10      1       01-10-2019 00:20:26     1
104     C       20      2       01-10-2019 14:40:45     2
105     B       21      3       02-10-2019 09:04:06     3
106     D       24      3       02-10-2019 09:04:12     4
107     A       24      3       02-10-2019 09:04:14     4
108     E       32      2       02-10-2019 09:04:20     4
109     A       10      1       02-10-2019 09:04:22     5
110     B       10      1       02-10-2019 10:40:49     6

第一时间栏:“01-10-2019 00:20:21”至“01-10-2019 00:20:30”, 下次bin:“01-10-2019 00:20:31”至“01-10-2019 00:20:40”, 下次bin:“01-10-2019 00:20:41”至“01-10-2019 00:20:50”, 下次bin:“01-10-2019 00:20:51”至“01-10-2019 00:21:00”, 下次bin:“01-10-2019 00:21:01”至“01-10-2019 00:21:10” 等等根据这些时间段,为每行分配“组”。 不强制要求有连续的组号(如果没有时间段,可以跳过该组号)

我已经使用for循环生成了这个,但是如果数据分布在几个月内,则需要很多时间。 请让我知道这是否可以作为一个熊猫操作使用一行代码来完成。谢谢


Tags: 数据key代码nameforbin时间group
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:34

下面是一个没有loop的示例。主要方法是将秒数取整到特定范围,并使用ngroup()

02-10-2019 09:04:12 -> 02-10-2019 09:04:11
02-10-2019 09:04:14 -> 02-10-2019 09:04:11
02-10-2019 09:04:20 -> 02-10-2019 09:04:11
02-10-2019 09:04:21 -> 02-10-2019 09:04:21
02-10-2019 09:04:25 -> 02-10-2019 09:04:21
...

我使用一个新的临时列来查找特定的范围

df = pd.DataFrame.from_dict({
    'Name': ('A', 'A', 'B', 'C', 'B', 'D', 'A', 'E', 'A', 'B'),
    'Val1': (1, 2, 1, 2, 3, 3, 3, 2, 1, 1),
    'Timestamp': (
        '2019-01-10 00:20:21',
        '2019-01-10 00:20:21',
        '2019-01-10 00:20:26',
        '2019-01-10 14:40:45',
        '2019-02-10 09:04:06',
        '2019-02-10 09:04:12',
        '2019-02-10 09:04:14',
        '2019-02-10 09:04:20',
        '2019-02-10 09:04:22',
        '2019-02-10 10:40:49',
    )
})
# convert str to Timestamp
df['Timestamp'] = pd.to_datetime(df['Timestamp'])

# your specific ranges. customize if you need
def sec_to_group(x):
    if 0 <= x.second <= 10:
        x = x.replace(second=0)
    elif 11 <= x.second <= 20:
        x = x.replace(second=11)
    elif 21 <= x.second <= 30:
        x = x.replace(second=21)
    elif 31 <= x.second <= 40:
        x = x.replace(second=31)
    elif 41 <= x.second <= 50:
        x = x.replace(second=41)
    elif 51 <= x.second <= 59:
        x = x.replace(second=51)
    return x


# new column formated_dt(temporary) with formatted seconds
df['formated_dt'] = df['Timestamp'].apply(sec_to_group)
# group by new column + ngroup() and drop
df['Group'] = df.groupby('formated_dt').ngroup()
df.drop(columns=['formated_dt'], inplace=True)
print(df)

输出:

#  Name  Val1           Timestamp  Group
# 0    A     1 2019-01-10 00:20:21      0  <- ngroup() calculates from 0
# 1    A     2 2019-01-10 00:20:21      0
# 2    B     1 2019-01-10 00:20:26      0
# 3    C     2 2019-01-10 14:40:45      1
# 4    B     3 2019-02-10 09:04:06      2
# ....

您也可以尝试使用TimeGrouper or resample

希望这有帮助

相关问题 更多 >

    热门问题