Python 将值添加到有效的dataframe并忽略无效的分析
在大量的数据帧中(示例如下所示),我想在Python 将值添加到有效的dataframe并忽略无效的分析,python,pandas,csv,Python,Pandas,Csv,在大量的数据帧中(示例如下所示),我想在'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
'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
我目前正在使用以下代码。但是,我不能跳过对带有字符串的dataframe的操作。如果我要使用errors='compresse'
,我将丢失包含字符串的数据帧
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='procure'
并在最终使用原始值替换缺少的值:
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
使用
errors='procure'
并在最终使用原始值替换缺少的值:
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