在python中,如何减去具有时间值的Excel文件的两列并为其创建新列?

时间:2020-01-29 10:21:24

标签: python excel dataframe time subtraction

我有一个像这样的excel数据库:

ExcelTimeData

我想使用python获取新列(ResponseTime)=(EndTime-StartTime)。我正在使用以下python代码:

BASH_REMATCH

它向我显示此错误:

n

如何在两列中减去时间值?

1 个答案:

答案 0 :(得分:0)

您要减去两个字符串,为了首先使用日期,必须使用.to_datetime

将列转换为datetime
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@FacesValidator(value = "customValidator")
public class CustomValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        //get input field value
        String consoleName = (String) value;

        //get some other value passed as component's attribute
        int pageTypeId = (Integer) component.getAttributes().get("pageTypeId");

        System.out.printf("Page type: %d, Console: %s\n", pageTypeId, consoleName);

        //if input is required (not null) for page 2, check if input is null
        if (pageTypeId == 2 && consoleName == null) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, "Input is required", ""));
        }

        //then implement any logic based on your requirements. For your unusual case,
        if (pageTypeId == 2) {
            if (!consoleName.equals("Sega")) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, "Sega is required for page type 2", ""));
            }
        }
    }
}

使用sf['EndTime'] = pd.to_datetime(sf['EndTime'], format='%H:%M:%S') sf['StartTime'] = pd.to_datetime(sf['StartTime'], format='%H:%M:%S') ,您可以更改字符串解析,检查strftime