gpt4 book ai didi

python - Pandas - 在 pandas.DataFrame() 中强制使用具有大量缺失值的数据的自动数据类型

转载 作者:行者123 更新时间:2023-12-01 07:15:11 24 4
gpt4 key购买 nike

当从如下列表的列表创建数据框时,所有值都获取类型object - 尽管大多数都是存储为字符串的int/floats。有没有办法强制执行数值的正确类型(int/float)?

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.dtypes
Out[88]:
Time object
Col2 object
Col3 object
Col4 object
Col5 object
Col6 object
Col7 object
Col8 object
Col9 object
Col10 object
Col11 object
Col12 object
Col13 object
dtype: object

最佳答案

您可以通过 DataFrame.set_indexTime 列转换为索引,然后如果可能的话转换为 timedeltas 并将所有列转换为数字 to_numeric使用 errors='coerce' 将非数值转换为 NaN:

df = df.set_index('Time')
df.index = pd.to_timedelta(df.index)
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 ,可以使用 try- except 创建自定义函数:

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', '']]
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.index = pd.to_timedelta(df.index)

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

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

关于python - Pandas - 在 pandas.DataFrame() 中强制使用具有大量缺失值的数据的自动数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58012458/

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