为有效的数据框添加值,并忽略无效的解析

时间:2019-05-21 09:18:19

标签: python pandas csv

在大量数据帧中(如下所示的示例),我想在'timestamp'列下的每个有效解析中添加一个指定值。但是, 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')

1 个答案:

答案 0 :(得分:0)

使用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