如何在Pandas中添加基于组的时间序列列?

2024-09-25 06:21:39 发布

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

link

上面是一个链接,指向我正在使用python修改的CSV文件的示例,我需要添加一个时间列,如果前一行的日期匹配,该时间列将增加1

如果日期更改,时间将从8:00:00重新开始

此外,如果“PL Seq”从G*变为H*,则时间也将从8开始

我想我已经记下了逻辑,只是很难把它写下来

在df“时间”中添加一列 将第一个“时间”值设置为8:00:00

读取df中的每一行

如果日期值=上一行的日期值,pl seq值第一个字符=第一个字符,则将时间值设置为时间+1

否则将时间值设置为时间

*请注意,我已经有了更改订单格式和目标状态日期的代码

当前

MODELCHASS,Prod Date,PL Seq
M742-021167,20200917,G0005
M359-020535,20200917,G0010
M742-022095,20200917,G0015
M220-001083,20200918,G0400
M742-022390,20200918,G0405
M907-004747,20200918,H0090
M934-005904,20200918,H0095

期望

MODELCHASS,Prod Date,PL Seq,Time
M742 021167,2020-09-17T,G0005,8:00:00
M359 020535,2020-09-17T,G0010,8:00:01
M742 022095,2020-09-17T,G0015,8:00:02
M220 001083,2020-09-18T,G0400,8:00:00
M742 022390,2020-09-18T,G0405,8:00:01
M907 004747,2020-09-18T,H0090,8:00:00
M934 005904,2020-09-18T,H0095,8:00:01

@Trenton如果H订单与G订单的日期相同,我们是否可以修改此选项 比如说

第6行中的当前编辑

MODELCHASS,Prod Date,PL Seq
M742-021167,20200917,G0005
M359-020535,20200917,G0010
M742-022095,20200917,G0015
M220-001083,20200918,G0400
M742-022390,20200918,G0405
M907-004747,20200917,H0090
M934-005904,20200917,H0095

预期编辑

MODELCHASS,Prod Date,PL Seq,Time
M742 021167,2020-09-17T,G0005,8:00:00
M359 020535,2020-09-17T,G0010,8:00:01
M742 022095,2020-09-17T,G0015,8:00:02
M220 001083,2020-09-18T,G0400,8:00:00
M742 022390,2020-09-18T,G0405,8:00:01
M907 004747,2020-09-17T,H0090,8:00:00
M934 005904,2020-09-17T,H0095,8:00:01

Tags: date时间prodseqplg0405m359g0400
1条回答
网友
1楼 · 发布于 2024-09-25 06:21:39
  • 'Prod Date'列转换为日期时间
  • 'Prod Date''PL Seq'对数据帧进行排序,因此'df'的加入顺序将与time_seq的加入顺序相同
  • 答案的主要部分是用.groupby.apply创建一个DateRange列表
  • .groupby{}和{}的第一个元素
    • df.groupby(['Prod Date', df['PL Seq'].str[0]])
  • .apply(lambda x: (pd.date_range(start=x.values[0] + pd.Timedelta(hours=8), periods=len(x), freq='s')).time)
    • 对于每个组,使用x中的第一个值作为startx.values[0]
      • 在此日期之前,添加一个8小时的Timedelta,以获取08:00:00
    • periods的数量为len[x]
    • freq's',持续几秒钟
    • 这将创建一个DateRange,从中使用.time提取时间
import pandas as pd

# setup test dataframe
data = {'MODELCHASS': ['M742-021167', 'M359-020535', 'M742-022095', 'M220-001083', 'M742-022390', 'M907-004747', 'M934-005904'],
        'Prod Date': [20200917, 20200917, 20200917, 20200918, 20200918, 20200918, 20200918],
        'PL Seq': ['G0005', 'G0010', 'G0015', 'G0400', 'G0405', 'H0090', 'H0095']}

df = pd.DataFrame(data)

# convert Prod Date to a datetime column
df['Prod Date'] = pd.to_datetime(df['Prod Date'], format='%Y%m%d')

# sort the dataframe by values so the order will correspond to the groupby order
df = df.sort_values(['Prod Date', 'PL Seq']).reset_index(drop=True)

# groupby Prod Date and the first character of PL Seq
# create a DateRange sequence for each group
# reshape the dataframe
time_seq = (df.groupby(['Prod Date', df['PL Seq'].str[0]])['Prod Date']
            .apply(lambda x: (pd.date_range(start=x.values[0] + pd.Timedelta(hours=8), periods=len(x), freq='s')).time)
            .reset_index(name='time_seq')
            .explode('time_seq')
            .reset_index(drop=True))

# join the time_seq column to df
df_new = df.join(time_seq.time_seq)

# display(df_new)
    MODELCHASS  Prod Date PL Seq  time_seq
0  M742-021167 2020-09-17  G0005  08:00:00
1  M359-020535 2020-09-17  G0010  08:00:01
2  M742-022095 2020-09-17  G0015  08:00:02
3  M220-001083 2020-09-18  G0400  08:00:00
4  M742-022390 2020-09-18  G0405  08:00:01
5  M907-004747 2020-09-18  H0090  08:00:00
6  M934-005904 2020-09-18  H0095  08:00:01

相关问题 更多 >