简体   繁体   English

Pandas - 添加一列,其值根据当前行和前一行中的另一列值计算得出

[英]Pandas - add a column with value computed based on another column value in current and previous row

Given the dataframe below,鉴于下面的 dataframe,

colNames = ["Time","Col2","Col3","Col4","Col5","Col6","Col7","Col8","Col9","Col10","Col11","Col12","Col13"]
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', '']]

df = pd.DataFrame(colVals, columns=colNames)
df = df.set_index('Time')
df = df.apply(pd.to_numeric, errors='coerce')

Need to add new Col14 , such that it starts from 0 and increment by one whenever value in Col11 of current row is less than the value in Col11 of previous row - if the previous row's Col11 value is NaN , it shouldn't increment Col14 .需要添加新的Col14 ,这样当当前行的Col11中的值小于前一行的Col11中的值时,它从 0 开始并递增一 - 如果前一行的Col11值为NaN ,则不应递增Col14

For example例如

+-------+-------+-------------------------------------------------------------------------------------------------+
| Col11 | Col14 |                                                                                                 |
+-------+-------+-------------------------------------------------------------------------------------------------+
| 900   |     0 | start with 0                                                                                    |
| NaN   |     0 |                                                                                                 |
| 900   |     0 |                                                                                                 |
| 903   |     0 |                                                                                                 |
| 904   |     0 |                                                                                                 |
| 904   |     0 |                                                                                                 |
| 8     |     1 | increment Col14 by 1 when current row's Col11 value is less than the previous row's Col11 value |
| 8     |     1 |                                                                                                 |
| 200   |     1 |                                                                                                 |
| 201   |     1 |                                                                                                 |
| NaN   |     1 | if Col11 is NaN, Col12 takes same value as prev row                                                                               |
| 0     |     2 | increment Col14 by 1 when current row's Col11 value is less than the previous row's Col11 value |
| 1     |     2 |                                                                                                 |
| NaN   |     2 |                                                                                                 |
| NaN   |     2 |                                                                                                 |
| 0     |     3 | increment Col14 by 1 when current row's Col11 value is less than the previous row's Col11 value |
+-------+-------+-------------------------------------------------------------------------------------------------+

Is there a way to get reference to the previous row?有没有办法参考上一行?

Can you try this?你能试试这个吗?

df['col14']=df.Col11.gt(df.Col11.shift(-1)).cumsum()

or或者

df['col14a']=df.Col11.gt(df.Col11.shift(-1)).shift().fillna(0).cumsum().astype(int)

The difference between the two is that the count switches at the end of lower value (in the first one) and it switches at the start of new value (2nd one)两者之间的区别在于计数在较低值的末尾(第一个)切换,它在新值的开始(第二个)切换

If you want to consider NaN same as the previous non-Nan value, you the code below.如果您想将 NaN 视为与之前的非 Nan 值相同,请使用下面的代码。

df['col14b']=df.Col11.fillna(method='ffill').gt(df.Col11.fillna(method='ffill').shift(-1)).shift().fillna(0).cumsum().astype(int)

With the smaller dummy dataset you provided, output is below使用您提供的较小的虚拟数据集,output 如下

Col11   col14   col14a  col14b
0   900.0   0   0   0
1   NaN     0   0   0
2   900.0   0   0   0
3   900.0   0   0   0
4   903.0   0   0   0
5   904.0   0   0   0
6   904.0   1   0   0
7   8.0     1   1   1
8   8.0     1   1   1
9   200.0   1   1   1
10  201.0   1   1   1
11  NaN     1   1   1
12  0.0     1   1   2
13  1.0     1   1   2
14  NaN     1   1   2
15  NaN     1   1   2
16  0.0     1   1   3

Details I am explaining the last one here, the other two are similar to this.细节我这里解释最后一个,其他两个与此类似。

With df.Col11.fillna(method='ffill') we are filling the NaN with the previous valid values, so tha out put is as below (we are not changing Coll11, we are just using this while creating the new column)使用df.Col11.fillna(method='ffill')我们用以前的有效值填充 NaN,所以输出如下(我们没有更改 Coll11,我们只是在创建新列时使用它)

0     900.0
1     900.0
2     900.0
3     900.0
4     903.0
5     904.0
6     904.0
7       8.0
8       8.0
9     200.0
10    201.0
11    201.0
12      0.0
13      1.0
14      1.0
15      1.0
16      0.0

With df.Col11.fillna(method='ffill').shift(-1) we are just shifting 1 row up so that we can do the comparison between the row & it's previous row.使用df.Col11.fillna(method='ffill').shift(-1)我们只是向上移动 1 行,以便我们可以在行和它的前一行之间进行比较。

0     900.0
1     900.0
2     900.0
3     903.0
4     904.0
5     904.0
6       8.0
7       8.0
8     200.0
9     201.0
10    201.0
11      0.0
12      1.0
13      1.0
14      1.0
15      0.0
16      NaN

With df.Col11.fillna(method='ffill').gt(df.Col11.fillna(method='ffill').shift(-1)) , we do the comparison & the result is true of false, as below.使用df.Col11.fillna(method='ffill').gt(df.Col11.fillna(method='ffill').shift(-1)) ,我们进行比较&结果为真假,如下.

0     False
1     False
2     False
3     False
4     False
5     False
6      True
7     False
8     False
9     False
10    False
11     True
12    False
13    False
14    False
15     True
16    False

We use shift to shift the value one row down since you want the number to switch when the smaller number is present.我们使用shift将值向下移动一行,因为您希望在较小的数字出现时切换数字。 When we shift rows down, the very first row becomes NaN , we are replacing that with 0 (since we have to start with zero) using .fillna(0) .当我们向下移动行时,第一行变为NaN ,我们使用.fillna(0) Then we use .cumsum() .然后我们使用.cumsum() .cumsum() adds only True values so, we get the values we need. .cumsum()只添加True值,所以我们得到了我们需要的值。 Finally, we assign this column as integer with .astype(int) to make all the values as integers.最后,我们将此列指定为 integer 和.astype(int)以使所有值都为整数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:基于另一列的前一行值的条件累积和 - Pandas: Conditional cumsum based on previous row value of another column 用 Pandas 中的前一行列填充当前行和列值 - Fill current row and column value with previous row column in Pandas 根据条件创建一个 Boolean 列,该列涉及 Pandas 中当前行和上一行的列的值 - Create a Boolean column based on condition which involves value from a column of the current row and previous row in Pandas 从Pandas列中的当前行值中减去前一行的值 - Subtract previous row value from the current row value in a Pandas column Pandas Dataframe基于前一行,将值添加到新列,但该列的最大值限于该列 - Pandas Dataframe Add a value to a new Column based on the previous row limited to the maximum value in that column 根据上一个行值创建一个新列并删除当前行 - Create a new column based on previous row value and delete the current row Pandas 如果存在基于另一列值的行,则删除列值 - Pandas delete a column value if there exists a row based on another column value 根据上一行另一列中的值修改1列中的值 - Modify values in 1 column based on the value in another column in the previous row 根据此行另一列与上一行的差确定一个值 - Determine a value based on the difference between another column of this row and the previous row Pandas - 添加列与行值应用条件相对于当前行 - Pandas - add column with row value applying conditions relative to current row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM