gpt4 book ai didi

python - 为有效数据帧添加值并忽略无效解析

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:21 25 4
gpt4 key购买 nike

在大量数据帧中(示例如下所示),我想在 'timestamp' 列下的每个有效解析中添加指定值。但是,时间戳数据帧包括数值和字符串。我想保留数据框中的原始字符串。

,Date,Time,Company,AV_ID,timestamp
0,29-Jan-2019,09:29:43.184,DEL,DEL0002,1548754413425000000
1,29-Jan-2019,09:29:43.184,in,msg:,should
2,29-Jan-2019,09:29:43.199,DEL,DEL0002,1548754413425000000
3,29-Jan-2019,09:29:43.199,in,msg:,should
4,29-Jan-2019,09:29:44.543,DEL,DEL0002,1548754415425000000
5,29-Jan-2019,09:29:44.543,in,msg:,should
6,29-Jan-2019,09:29:44.574,DEL,DEL0002,1548754415425000000
7,29-Jan-2019,09:29:44.574,in,msg:,should
8,29-Jan-2019,09:29:46.606,DEL,DEL0002,1548754417425000000

我目前正在使用以下代码。但是,我无法跳过带有字符串的数据帧的操作。如果我使用 errors='coerce',我将丢失包含字符串的数据帧。

local = 28800000
orig_data['timestamp'] = pd.to_numeric(orig_data['timestamp'], errors = 'ignore')
orig_data['timestamp'] = orig_data['timestamp'] + local
orig_data['timestamp'] = pd.to_datetime(orig_data['timestamp'], unit = 'ms')

最佳答案

使用 errors = 'coerce' 并在最终用原始值替换缺失值:

local = 28800000
s = pd.to_numeric(orig_data['timestamp'], errors = 'coerce') + local
#change unit to ns
orig_data['timestamp'] = pd.to_datetime(s, unit = 'ns').fillna(orig_data['timestamp'])

print (orig_data)
Date Time Company AV_ID timestamp
0 29-Jan-2019 09:29:43.184 DEL DEL0002 2019-01-29 09:33:33.453799936
1 29-Jan-2019 09:29:43.184 in msg: should
2 29-Jan-2019 09:29:43.199 DEL DEL0002 2019-01-29 09:33:33.453799936
3 29-Jan-2019 09:29:43.199 in msg: should
4 29-Jan-2019 09:29:44.543 DEL DEL0002 2019-01-29 09:33:35.453799936
5 29-Jan-2019 09:29:44.543 in msg: should
6 29-Jan-2019 09:29:44.574 DEL DEL0002 2019-01-29 09:33:35.453799936
7 29-Jan-2019 09:29:44.574 in msg: should
8 29-Jan-2019 09:29:46.606 DEL DEL0002 2019-01-29 09:33:37.453799936

关于python - 为有效数据帧添加值并忽略无效解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235082/

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