[Python] Pandas comparing each date in a column with other and setting a flag if it isn't?

Discussão em 'Python' iniciado por Stack, Julho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I was going through some YouTube tutorials on Pandas dataframe and came across a problem when I tried filtering dates on a column.

    I made a dummy csv file like this:

    Name,State,Date
    Mike,Kansas,April7 2018
    Charlie,Dubai,7April2018
    Amanda,Oregon,07-04-2018
    Samantha,Florida,07-04-2018
    Tivan,Miami,07-04-2018
    Pedro,Mexico,07-04-2018
    Lionel,NYC,07-04-2018
    Claire,Washington,07-04-2018
    Linsday,Texas,07-04-2018


    From that .csv, what I was trying to do was to find a way so that all dates are compared to check if they are all of the same format and if they aren't then I need to set a flag for the same.

    Here is my code:

    import pandas as pd
    df = pd.read_csv("SampleFile.csv")
    date_parameter = df["Date"][0]
    date_list = df["Date"].tolist()
    flag = 0
    for x in date_list:
    if x == date_parameter:
    # both captured date and dates in "Date" oolumn
    flag = 0
    else:
    # if the date captured isn't consistent ie not of a single date format
    # then set flag
    flag +=1
    print "Finished searching file, the value of the flag is:\n", flag


    But it always shows the value as 0 - how can I correct my mistake, or is there a better way to do this? Thanks in advance

    Continue reading...

Compartilhe esta Página