熊猫-将列转换为int并强制NaN

时间:2019-09-20 13:29:48

标签: python pandas dataframe

给出如下所示的数据框

colVals = [['05:17:55.703', '', '', '', '', '', '21', '', '3', '89', '891', '11', ''], ['05:17:55.703', '', '', '', '', '', '21', '', '3', '217', '891', '12', ''], ['05:17:55.703', '', '', '', '', '', '21', '', '3', '217', '891', '13', ''], ['05:17:55.703', '', '', '', '', '', '21', '', '3', '217', '891', '15', ''], ['05:17:55.703', '', '', '', '', '', '21', '', '3', '217', '891', '16', ''], ['05:17:55.703', '', '', '', '', '', '21', '', '3', '217', '891', '17', ''], ['05:17:55.703', '', '', '', '', '', '21', '', '3', '217', '891', '18', ''], ['05:17:55.707', '', '', '', '', '', '18', '', '3', '185', '892', '0', ''], ['05:17:55.707', '', '', '', '', '', '21', '', '3', '185', '892', '1', ''], ['05:17:55.707', '', '', '', '', '', '17', '', '3', '73', '892', '5', ''], ['05:17:55.707', '', '', '', '', '', '17', '', '3', '185', '892', '6', ''], ['05:17:55.707', '', '', '', '', '', '21', '', '3', '73', '892', '7', ''], ['05:17:55.708', '268', '4', '28', '-67.60', '13', '', '2', '', '', '', '', '2'], ['05:17:55.711', '', '', '', '', '', '18', '', '3', '57', '892', '10', ''], ['05:17:55.711', '', '', '', '', '', '21', '', '3', '201', '892', '11', ''], ['05:17:55.711', '', '', '', '', '', '21', '', '3', '25', '892', '12', ''], ['05:17:55.723', '', '', '', '', '', '21', '', '3', '217', '893', '11', ''], ['05:17:55.723', '', '', '', '', '', '21', '', '3', '217', '893', '15', ''], ['05:17:55.723', '', '', '', '', '', '21', '', '3', '217', '893', '16', ''], ['05:17:55.726', '268', '4', '', '-67.80', '', '', '', '', '', '', '', ''], ['05:17:55.728', '', '', '28', '', '12', '31', '2', '3', '185', '894', '0', '1'], ['05:17:55.728', '', '', '', '', '', '31', '', '3', '185', '894', '1', ''], ['05:17:55.731', '', '', '', '', '', '31', '', '3', '217', '894', '10', ''], ['05:17:55.731', '', '', '', '', '', '20', '', '3', '217', '894', '11', ''], ['05:17:55.731', '', '', '', '', '', '20', '', '3', '217', '894', '12', ''], ['05:17:55.731', '', '', '', '', '', '20', '', '3', '217', '894', '13', ''], ['05:17:55.743', '', '', '', '', '', '20', '', '3', '217', '895', '11', ''], ['05:17:55.743', '', '', '', '', '', '20', '', '3', '217', '895', '15', ''], ['05:17:55.743', '', '', '', '', '', '20', '', '3', '217', '895', '16', ''], ['05:17:55.746', '268', '4', '', '-67.82', '', '', '', '', '', '', '', ''], ['05:17:55.747', '', '', '28', '', '13', '20', '2', '3', '185', '896', '1', '2'], ['05:17:55.747', '', '', '', '', '', '20', '', '3', '185', '896', '2', ''], ['05:17:55.747', '', '', '', '', '', '30', '', '3', '217', '896', '5', ''], ['05:17:55.751', '', '', '', '', '', '18', '', '3', '217', '896', '10', ''], ['05:17:55.751', '', '', '', '', '', '21', '', '3', '217', '896', '11', ''], ['05:17:55.751', '', '', '', '', '', '21', '', '3', '217', '896', '12', ''], ['05:17:55.751', '', '', '', '', '', '21', '', '3', '217', '896', '13', ''], ['05:17:55.763', '', '', '', '', '', '31', '', '3', '217', '897', '11', ''], ['05:17:55.763', '', '', '', '', '', '30', '', '3', '217', '897', '15', ''], ['05:17:55.763', '', '', '', '', '', '20', '', '3', '217', '897', '16', ''], ['05:17:55.763', '', '', '', '', '', '20', '', '3', '217', '897', '17', ''], ['05:17:55.766', '268', '4', '', '-67.13', '', '', '', '', '', '', '', ''], ['05:17:55.768', '', '', '28', '', '12', '20', '2', '3', '185', '898', '3', '2'], ['05:17:55.768', '', '', '', '', '', '16', '', '3', '217', '898', '6', ''], ['05:17:55.771', '', '', '', '', '', '18', '', '3', '217', '898', '10', ''], ['05:17:55.771', '', '', '', '', '', '20', '', '3', '217', '898', '11', ''], ['05:17:55.771', '', '', '', '', '', '20', '', '3', '217', '898', '12', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '217', '899', '11', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '41', '899', '12', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '25', '899', '13', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '217', '899', '15', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '217', '899', '16', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '217', '899', '17', ''], ['05:17:55.784', '', '', '', '', '', '20', '', '3', '217', '899', '18', ''], ['05:17:55.786', '268', '4', '', '-67.66', '', '', '', '', '', '', '', ''], ['05:17:55.788', '', '', '28', '', '13', '18', '2', '3', '185', '900', '0', '2'], ['05:17:55.788', '', '', '', '', '', '20', '', '3', '185', '900', '1', ''], ['05:17:55.788', '', '', '', '', '', '20', '', '3', '185', '900', '2', ''], ['05:17:55.788', '', '', '', '', '', '16', '', '3', '41', '900', '5', ''], ['05:17:55.788', '', '', '', '', '', '17', '', '3', '185', '900', '6', ''], ['05:17:55.791', '', '', '', '', '', '20', '', '3', '105', '900', '7', ''], ['05:17:55.791', '', '', '', '', '', '20', '', '3', '89', '900', '8', ''], ['05:17:55.791', '', '', '', '', '', '18', '', '3', '217', '900', '10', ''], ['05:17:55.791', '', '', '', '', '', '20', '', '3', '217', '900', '11', ''], ['05:17:55.791', '', '', '', '', '', '20', '', '3', '25', '900', '12', ''], ['05:17:55.806', '268', '4', '', '-67.50', '', '', '', '', '', '', '', ''], ['05:17:55.808', '', '', '28', '', '12', '31', '2', '3', '185', '902', '0', '1'], ['05:17:55.808', '', '', '', '', '', '31', '', '3', '185', '902', '1', ''], ['05:17:55.808', '', '', '', '', '', '20', '', '3', '25', '902', '2', ''], ['05:17:55.808', '', '', '', '', '', '20', '', '3', '25', '902', '3', ''], ['05:17:55.808', '', '', '', '', '', '16', '', '3', '217', '902', '5', ''], ['05:17:55.808', '', '', '', '', '', '16', '', '3', '217', '902', '6', ''], ['05:17:55.811', '', '', '', '', '', '20', '', '3', '89', '902', '7', ''], ['05:17:55.811', '', '', '', '', '', '20', '', '3', '121', '902', '8', ''], ['05:17:55.811', '', '', '', '', '', '18', '', '3', '217', '902', '10', ''], ['05:17:55.811', '', '', '', '', '', '20', '', '3', '217', '902', '11', ''], ['05:17:55.811', '', '', '', '', '', '20', '', '3', '73', '902', '12', ''], ['05:17:55.811', '', '', '', '', '', '20', '', '3', '9', '902', '15', ''], ['05:17:55.815', '', '', '', '', '', '20', '', '3', '217', '902', '16', ''], ['05:17:55.815', '', '', '', '', '', '20', '', '3', '25', '902', '17', ''], ['05:17:55.815', '', '', '', '', '', '20', '', '3', '217', '902', '18', ''], ['05:17:55.815', '', '', '', '', '', '18', '', '3', '217', '903', '0', ''], ['05:17:55.815', '', '', '', '', '', '21', '', '3', '217', '903', '1', ''], ['05:17:55.815', '', '', '', '', '', '19', '', '3', '105', '903', '2', ''], ['05:17:55.815', '', '', '', '', '', '21', '', '3', '41', '903', '3', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '217', '903', '11', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '9', '903', '12', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '105', '903', '13', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '217', '903', '15', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '217', '903', '16', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '121', '903', '17', ''], ['05:17:55.823', '', '', '', '', '', '21', '', '3', '89', '903', '18', ''], ['05:17:55.826', '268', '4', '', '-67.51', '', '', '', '', '', '', '', ''], ['05:17:55.828', '', '', '28', '', '12', '18', '2', '3', '185', '904', '0', '1'], ['05:17:55.828', '', '', '', '', '', '21', '', '3', '185', '904', '1', ''], ['05:17:55.828', '', '', '', '', '', '21', '', '3', '185', '904', '2', ''], ['05:17:55.828', '', '', '', '', '', '21', '', '3', '185', '904', '3', ''], ['05:17:55.828', '', '', '', '', '', '17', '', '3', '217', '904', '5', ''], ['05:17:55.828', '', '', '', '', '', '17', '', '3', '217', '904', '6', ''], ['05:17:55.831', '', '', '', '', '', '21', '', '3', '217', '904', '7', ''], ['05:17:55.831', '', '', '', '', '', '20', '', '3', '169', '904', '11', ''], ['05:17:55.831', '', '', '', '', '', '20', '', '3', '217', '904', '12', ''], ['05:17:55.831', '', '', '', '', '', '20', '', '3', '217', '904', '13', ''], ['05:17:55.846', '268', '4', '', '-67.01', '', '', '', '', '', '', '', ''], ['05:17:55.848', '', '', '28', '', '13', '19', '2', '3', '57', '906', '1', '2'], ['05:17:55.848', '', '', '', '', '', '19', '', '3', '41', '906', '2', ''], ['05:17:55.848', '', '', '', '', '', '19', '', '3', '73', '906', '3', ''], ['05:17:55.848', '', '', '', '', '', '16', '', '3', '217', '906', '5', ''], ['05:17:55.848', '', '', '', '', '', '16', '', '3', '217', '906', '6', ''], ['05:17:55.848', '', '', '', '', '', '19', '', '3', '9', '906', '7', ''], ['05:17:55.851', '', '', '', '', '', '20', '', '3', '121', '906', '11', ''], ['05:17:55.851', '', '', '', '', '', '20', '', '3', '57', '906', '12', ''], ['05:17:55.851', '', '', '', '', '', '20', '', '3', '105', '906', '13', ''], ['05:17:55.855', '', '', '', '', '', '20', '', '3', '217', '906', '15', ''], ['05:17:55.855', '', '', '', '', '', '20', '', '3', '217', '906', '16', ''], ['05:17:55.855', '', '', '', '', '', '20', '', '3', '105', '906', '17', ''], ['05:17:55.855', '', '', '', '', '', '17', '', '3', '185', '907', '0', ''], ['05:17:55.855', '', '', '', '', '', '20', '', '3', '217', '907', '1', ''], ['05:17:55.855', '', '', '', '', '', '20', '', '3', '9', '907', '2', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '217', '907', '11', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '57', '907', '12', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '153', '907', '13', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '217', '907', '15', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '217', '907', '16', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '57', '907', '17', ''], ['05:17:55.864', '', '', '', '', '', '20', '', '3', '105', '907', '18', ''], ['05:17:55.867', '', '', '', '', '', '17', '', '3', '185', '908', '0', ''], ['05:17:55.867', '', '', '', '', '', '20', '', '3', '185', '908', '1', ''], ['05:17:55.867', '', '', '', '', '', '20', '', '3', '9', '908', '2', ''], ['05:17:55.867', '', '', '', '', '', '16', '', '3', '73', '908', '6', ''], ['05:17:55.868', '268', '4', '28', '-66.79', '13', '', '2', '', '', '', '', '2'], ['05:17:55.871', '', '', '', '', '', '20', '', '3', '105', '908', '7', ''], ['05:17:55.871', '', '', '', '', '', '20', '', '3', '25', '908', '8', ''], ['05:17:55.871', '', '', '', '', '', '17', '', '3', '217', '908', '10', ''], ['05:17:55.871', '', '', '', '', '', '21', '', '3', '217', '908', '11', ''], ['05:17:55.871', '', '', '', '', '', '21', '', '3', '9', '908', '12', ''], ['05:17:55.871', '', '', '', '', '', '21', '', '3', '121', '908', '13', ''], ['05:17:55.875', '', '', '', '', '', '21', '', '3', '217', '908', '15', ''], ['05:17:55.875', '', '', '', '', '', '21', '', '3', '217', '908', '16', ''], ['05:17:55.875', '', '', '', '', '', '21', '', '3', '57', '908', '17', ''], ['05:17:55.875', '', '', '', '', '', '18', '', '3', '73', '909', '0', ''], ['05:17:55.875', '', '', '', '', '', '21', '', '3', '217', '909', '1', ''], ['05:17:55.875', '', '', '', '', '', '21', '', '3', '89', '909', '2', ''], ['05:17:55.875', '', '', '', '', '', '21', '', '3', '89', '909', '3', ''], ['05:17:55.886', '268', '4', '', '-67.48', '', '', '', '', '', '', '', ''], ['05:17:55.888', '', '', '28', '', '12', '31', '2', '3', '185', '910', '0', '1'], ['05:17:55.888', '', '', '', '', '', '21', '', '3', '89', '910', '1', ''], ['05:17:55.888', '', '', '', '', '', '21', '', '3', '9', '910', '2', ''], ['05:17:55.888', '', '', '', '', '', '21', '', '3', '185', '910', '3', ''], ['05:17:55.888', '', '', '', '', '', '17', '', '3', '217', '910', '5', ''], ['05:17:55.888', '', '', '', '', '', '17', '', '3', '217', '910', '6', ''], ['05:17:55.891', '', '', '', '', '', '21', '', '3', '89', '910', '7', ''], ['05:17:55.891', '', '', '', '', '', '21', '', '3', '73', '910', '8', ''], ['05:17:55.891', '', '', '', '', '', '18', '', '3', '217', '910', '10', ''], ['05:17:55.891', '', '', '', '', '', '21', '', '3', '217', '910', '11', ''], ['05:17:55.891', '', '', '', '', '', '21', '', '3', '57', '910', '12', ''], ['05:17:55.900', '', '', '', '', '', '21', '', '3', '217', '911', '3', ''], ['05:17:55.900', '', '', '', '', '', '17', '', '3', '217', '911', '5', ''], ['05:17:55.900', '', '', '', '', '', '17', '', '3', '217', '911', '6', ''], ['05:17:55.900', '', '', '', '', '', '21', '', '3', '105', '911', '7', ''], ['05:17:55.900', '', '', '', '', '', '21', '', '3', '25', '911', '8', ''], ['05:17:55.900', '', '', '', '', '', '18', '', '3', '217', '911', '10', ''], ['05:17:55.903', '', '', '', '', '', '21', '', '3', '217', '911', '11', ''], ['05:17:55.903', '', '', '', '', '', '21', '', '3', '57', '911', '12', ''], ['05:17:55.903', '', '', '', '', '', '21', '', '3', '153', '911', '13', ''], ['05:17:55.903', '', '', '', '', '', '21', '', '3', '217', '911', '15', ''], ['05:17:55.903', '', '', '', '', '', '21', '', '3', '217', '911', '16', ''], ['05:17:55.903', '', '', '', '', '', '21', '', '3', '137', '911', '17', ''], ['05:17:55.906', '268', '4', '', '-67.79', '', '', '', '', '', '', '', ''], ['05:17:55.908', '', '', '28', '', '13', '18', '2', '3', '57', '912', '0', '2'], ['05:17:55.908', '', '', '', '', '', '21', '', '3', '185', '912', '1', ''], ['05:17:55.908', '', '', '', '', '', '21', '', '3', '57', '912', '2', ''], ['05:17:55.908', '', '', '', '', '', '17', '', '3', '137', '912', '5', ''], ['05:17:55.908', '', '', '', '', '', '17', '', '3', '169', '912', '6', ''], ['05:17:55.923', '', '', '', '', '', '21', '', '3', '217', '913', '11', ''], ['05:17:55.923', '', '', '', '', '', '21', '', '3', '89', '913', '12', ''], ['05:17:55.926', '268', '4', '', '-68.13', '', '', '', '', '', '', '', ''], ['05:17:55.928', '', '', '28', '', '13', '31', '2', '3', '185', '914', '0', '2'], ['05:17:55.928', '', '', '', '', '', '21', '', '3', '105', '914', '2', ''], ['05:17:55.928', '', '', '', '', '', '17', '', '3', '25', '914', '5', ''], ['05:17:55.928', '', '', '', '', '', '17', '', '3', '121', '914', '6', ''], ['05:17:55.928', '', '', '', '', '', '21', '', '3', '25', '914', '7', ''], ['05:17:55.931', '', '', '', '', '', '18', '', '3', '89', '914', '10', ''], ['05:17:55.931', '', '', '', '', '', '21', '', '3', '217', '914', '11', ''], ['05:17:55.931', '', '', '', '', '', '21', '', '3', '73', '914', '12', ''], ['05:17:55.939', '', '', '', '', '', '17', '', '3', '137', '915', '5', ''], ['05:17:55.939', '', '', '', '', '', '17', '', '3', '153', '915', '6', ''], ['05:17:55.939', '', '', '', '', '', '21', '', '3', '73', '915', '7', ''], ['05:17:55.939', '', '', '', '', '', '18', '', '3', '153', '915', '10', ''], ['05:17:55.943', '', '', '', '', '', '21', '', '3', '217', '915', '11', ''], ['05:17:55.943', '', '', '', '', '', '21', '', '3', '137', '915', '12', ''], ['05:17:55.943', '', '', '', '', '', '21', '', '3', '137', '915', '15', ''], ['05:17:55.943', '', '', '', '', '', '21', '', '3', '217', '915', '16', ''], ['05:17:55.943', '', '', '', '', '', '21', '', '3', '105', '915', '17', ''], ['05:17:55.946', '268', '4', '', '-67.45', '', '', '', '', '', '', '', ''], ['05:17:55.948', '', '', '28', '', '9', '30', '3', '3', '160', '916', '0', '3'], ['05:17:55.948', '', '', '', '', '', '21', '', '3', '121', '916', '1', ''], ['05:17:55.948', '', '', '', '', '', '21', '', '3', '105', '916', '2', ''], ['05:17:55.948', '', '', '', '', '', '17', '', '3', '73', '916', '5', ''], ['05:17:55.948', '', '', '', '', '', '17', '', '3', '137', '916', '6', ''], ['05:17:55.959', '', '', '', '', '', '13', '', '4', '217', '917', '3', ''], ['05:17:55.959', '', '', '', '', '', '11', '', '4', '217', '917', '5', ''], ['05:17:55.959', '', '', '', '', '', '11', '', '4', '217', '917', '6', ''], ['05:17:55.959', '', '', '', '', '', '13', '', '4', '217', '917', '7', ''], ['05:17:55.959', '', '', '', '', '', '13', '', '4', '217', '917', '8', ''], ['05:17:55.959', '', '', '', '', '', '11', '', '4', '153', '917', '10', ''], ['05:17:55.963', '', '', '', '', '', '13', '', '4', '217', '917', '11', ''], ['05:17:55.963', '', '', '', '', '', '13', '', '4', '121', '917', '12', ''], ['05:17:55.966', '268', '4', '', '-67.22', '', '', '', '', '', '', '', ''], ['05:17:55.968', '', '', '28', '', '12', '14', '2', '4', '57', '918', '1', '2'], ['05:17:55.968', '', '', '', '', '', '14', '', '4', '185', '918', '2', ''], ['05:17:55.968', '', '', '', '', '', '12', '', '4', '169', '918', '5', ''], ['05:17:55.983', '', '', '', '', '', '14', '', '4', '57', '919', '11', ''], ['05:17:55.983', '', '', '', '', '', '14', '', '4', '217', '919', '12', ''], ['05:17:55.983', '', '', '', '', '', '14', '', '4', '9', '919', '15', ''], ['05:17:55.983', '', '', '', '', '', '14', '', '4', '217', '919', '16', ''], ['05:17:55.983', '', '', '', '', '', '14', '', '4', '217', '919', '17', ''], ['05:17:55.986', '268', '4', '', '-67.80', '', '', '', '', '', '', '', ''], ['05:17:55.988', '', '', '28', '', '13', '14', '2', '4', '9', '920', '1', '1'], ['05:17:55.988', '', '', '', '', '', '14', '', '4', '41', '920', '2', ''], ['05:17:55.988', '', '', '', '', '', '14', '', '4', '57', '920', '3', ''], ['05:17:55.988', '', '', '', '', '', '12', '', '4', '217', '920', '5', ''], ['05:17:55.988', '', '', '', '', '', '12', '', '4', '217', '920', '6', ''], ['05:17:55.988', '', '', '', '', '', '14', '', '4', '41', '920', '7', ''], ['05:17:55.991', '', '', '', '', '', '14', '', '4', '89', '920', '8', ''], ['05:17:55.991', '', '', '', '', '', '22', '', '3', '217', '920', '11', ''], ['05:17:55.991', '', '', '', '', '', '22', '', '3', '89', '920', '12', ''], ['05:17:56.003', '', '', '', '', '', '22', '', '3', '25', '921', '11', ''], ['05:17:56.003', '', '', '', '', '', '22', '', '3', '121', '921', '12', ''], ['05:17:56.003', '', '', '', '', '', '22', '', '3', '25', '921', '13', ''], ['05:17:56.003', '', '', '', '', '', '22', '', '3', '217', '921', '15', ''], ['05:17:56.003', '', '', '', '', '', '22', '', '3', '217', '921', '16', ''], ['05:17:56.003', '', '', '', '', '', '22', '', '3', '217', '921', '17', ''], ['05:17:56.006', '268', '4', '', '-67.31', '', '', '', '', '', '', '', ''], ['05:17:56.008', '', '', '28', '', '10', '22', '3', '3', '9', '922', '1', '3'], ['05:17:56.008', '', '', '', '', '', '22', '', '3', '73', '922', '2', ''], ['05:17:56.008', '', '', '', '', '', '17', '', '3', '121', '922', '5', ''], ['05:17:56.008', '', '', '', '', '', '17', '', '3', '185', '922', '6', ''], ['05:17:56.011', '', '', '', '', '', '22', '', '3', '217', '922', '7', ''], ['05:17:56.011', '', '', '', '', '', '22', '', '3', '121', '922', '11', ''], ['05:17:56.011', '', '', '', '', '', '22', '', '3', '89', '922', '12', ''], ['05:17:56.020', '', '', '', '', '', '17', '', '3', '105', '923', '5', ''], ['05:17:56.020', '', '', '', '', '', '17', '', '3', '185', '923', '6', ''], ['05:17:56.020', '', '', '', '', '', '22', '', '3', '25', '923', '7', ''], ['05:17:56.020', '', '', '', '', '', '31', '', '3', '112', '923', '10', ''], ['05:17:56.023', '', '', '', '', '', '31', '', '3', '112', '923', '11', ''], ['05:17:56.023', '', '', '', '', '', '22', '', '3', '217', '923', '12', ''], ['05:17:56.023', '', '', '', '', '', '22', '', '3', '217', '923', '13', ''], ['05:17:56.023', '', '', '', '', '', '22', '', '3', '217', '923', '15', ''], ['05:17:56.023', '', '', '', '', '', '22', '', '3', '217', '923', '16', ''], ['05:17:56.023', '', '', '', '', '', '22', '', '3', '217', '923', '17', ''], ['05:17:56.023', '', '', '', '', '', '22', '', '3', '217', '923', '18', ''], ['05:17:56.027', '', '', '', '', '', '18', '', '3', '57', '924', '0', ''], ['05:17:56.027', '', '', '', '', '', '22', '', '3', '41', '924', '1', ''], ['05:17:56.027', '', '', '', '', '', '22', '', '3', '185', '924', '2', ''], ['05:17:56.027', '', '', '', '', '', '22', '', '3', '185', '924', '3', ''], ['05:17:56.027', '', '', '', '', '', '17', '', '3', '73', '924', '5', ''], ['05:17:56.027', '', '', '', '', '', '17', '', '3', '217', '924', '6', ''], ['05:17:56.028', '268', '4', '28', '-67.68', '13', '', '2', '', '', '', '', '2'], ['05:17:56.039', '', '', '', '', '', '22', '', '3', '217', '925', '3', ''], ['05:17:56.039', '', '', '', '', '', '17', '', '3', '217', '925', '5', ''], ['05:17:56.039', '', '', '', '', '', '17', '', '3', '217', '925', '6', ''], ['05:17:56.039', '', '', '', '', '', '22', '', '3', '217', '925', '7', ''], ['05:17:56.039', '', '', '', '', '', '18', '', '3', '57', '925', '10', ''], ['05:17:56.043', '', '', '', '', '', '22', '', '3', '73', '925', '16', '']]
colNames = ["Time","Col2","Col3","Col4","Col5","Col6","Col7","Col8","Col9","Col10","Col11","Col12","Col13"]
df = pd.DataFrame(colVals, columns=colNames)
df = df.set_index('Time')
df = df.apply(pd.to_numeric, errors='coerce')


    print (df.dtypes)
    Col2     float64
    Col3     float64
    Col4     float64
    Col5     float64
    Col6     float64
    Col7     float64
    Col8     float64
    Col9     float64
    Col10    float64
    Col11    float64
    Col12    float64
    Col13    float64
    dtype: object

您可能会在这里注意到,所有列均已转换为float类型。有没有一种方法可以将某些列转换为int

尝试了以下

type_dict = {'Col2': int}
df.astype(dtype_dict)

这将引发错误ValueError: Cannot convert non-finite values (NA or inf) to integer

阅读有关Nullable integer data type并尝试过

dtype_dict = {'Col2': 'Int64'}
df.astype(dtype_dict)

这会引发错误ValueError: Cannot convert non-finite values (NA or inf) to integer

2 个答案:

答案 0 :(得分:1)

我认为您的熊猫版本存在问题,如果使用pandas 0.24+和整数列,则效果很好:

df = df.apply(pd.to_numeric, errors='coerce')
dtype_dict = {'Col2': 'Int64'}
print (df.astype(dtype_dict).dtypes)
Col2       Int64
Col3     float64
Col4     float64
Col5     float64
Col6     float64
Col7     float64
Col8     float64
Col9     float64
Col10    float64
Col11    float64
Col12    float64
Col13    float64
dtype: object

但是如果要使用float(此处为Col5)填充数字的转换列,则会失败:

df = df.apply(pd.to_numeric, errors='coerce')

print (df['Col5'].tolist())
[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, -67.6, 
 nan, nan, nan, nan, nan, nan, -67.8, nan, nan, nan, nan, nan, nan, 
 nan, nan, nan, -67.82, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 nan, nan, -67.13, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 nan, nan, -67.66, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 -67.5, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 -67.51, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, -67.01, 
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 -66.79, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 nan, nan, -67.48, nan, nan, nan, nan, nan, nan, nan, nan, nan,
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan,
 -67.79, nan, nan, nan, nan, nan, nan, nan, -68.13, nan, nan, nan, nan,
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, -67.45,
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, -67.22,
 nan, nan, nan, nan, nan, nan, nan, nan, -67.8, nan, nan, nan, nan, nan, 
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, -67.31, nan, nan, nan,
 nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, 
 nan, nan, nan, nan, nan, nan, nan, -67.68, nan, nan, nan, nan, nan, nan]

dtype_dict = {'Col5': 'Int64'}
print (df.astype(dtype_dict).dtypes)
  

TypeError:无法安全地将非等效的float64强制转换为int64

如果想将所有列都转换为整数(如果不是浮点数的话):

df = df.set_index('Time')


def func(x):
    a = pd.to_numeric(x, errors='coerce')
    try:
        return a.astype('Int64')
    except:
        return a

df1 = df.apply(func)

print (df1.dtypes)
Col2       Int64
Col3       Int64
Col4       Int64
Col5     float64
Col6       Int64
Col7       Int64
Col8       Int64
Col9       Int64
Col10      Int64
Col11      Int64
Col12      Int64
Col13      Int64
dtype: object

答案 1 :(得分:0)

在将行转换为int之前,您必须用0填充

您可以这样做


df[["Col2","Col3","Col4","Col5","Col6"]] = df[["Col2","Col3","Col4","Col5","Col6"]].fillna(0).astype(np.int32, copy=False)

display(df)