lundi 18 mai 2020

How do I create a calculated series based on if else using data in columns and rows using Pandas

Below is a dataframe where I calculate the time difference between a preceding row STime and the current row STime. However, what I really wish to do is to populate this timeDiffTemp column based on other conditions such as having the same date. Yes, I am brand new to pandas - and realize this is trivial to many of you. So... thanks in advance for your gracious help.

'''

         SDate     STime      timeDiffTemp
0   2017-12-06  07:00:00               NaT
1   2017-12-06  09:55:00          02:55:00
2   2017-12-06  10:25:00          00:30:00
3   2017-12-06  15:30:00          05:05:00
4   2017-12-07  10:00:00 -1 days +18:30:00

'''

'''

df["STime"] = pd.to_datetime(df["STime"])  # convert STime to a datetime field
df["SDate"] = pd.to_datetime(df["SDate"])  # convert STime to a datetime field
pd.to_datetime(df['timeDiffTemp']) = df['STime'] - df['STime'].shift(1)
df['timeDiff'] = df['timeDiffTemp'].apply(lambda x: df['timeDiffTemp'] if (df['SDate'] == df['SDate'].shift(1)) else 'NA')

'''

Aucun commentaire:

Enregistrer un commentaire