上面是一个链接,指向我正在使用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订单的日期相同,我们是否可以修改此选项 比如说
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
'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)
start
:x.values[0]
08:00:00
periods
的数量为len[x]
freq
是's'
,持续几秒钟李>DateRange
,从中使用.time
提取时间相关问题 更多 >
编程相关推荐