mercredi 13 juillet 2016

Python - Iterrow through pandas dataframe and assign and conditionally update datetime variable

I'm a Python novice and was wondering if anyone could help me.

I want to iterate through datetime column in a pandas data frame, while for each iteration update a variable with the most recent time. Let's assume this is my data:

    Time
06:12:50
06:13:51
06:13:51
06:13:50
06:14:51
06:14:49

For my result, I want it to look something like this:

RecentTime:
   06:12:50
   06:13:51
   06:13:51
   06:13:51
   06:14:51
   06:14:51

I think the code should look something like this, but I have had trouble with it and can't figure out why. This is my code:

RecentTime = [] # Store list of most recent time for each row
Index: None       # Create empty variable
# Loop through 
for index, row in data.iterrows():
    index = row['Time']   # Save value as index
    if index >= row['Time']: # If time is greater than current row
    index = row['Time']
        RecentTime.append(index) # Append most recent variable into list
    else:
        continue

For some reason, this is my result:

RecentTime
  06:12:50
  06:13:51
  06:13:51
  06:13:50
  06:14:51
  06:14:49

Aucun commentaire:

Enregistrer un commentaire