0

I have a python program to generate excel reports. It creates an additional column next to a column where logic condition is met. The header of this column has to contain the header (field label) of column where condition is met. Currently the header of this column is always "Duration", but I want it to be '"header of column where condition was met" + space + "Duration"'. Header of column where condition was met is 'Field_Label' in CSV file. For example the header of column where condition is met is "Order Date", the header of new column has to be "Order Date Duration". Below is portion of code that inserts this column. I would appreciate any ideas. The expected output is number of days between 2 dates.

'''
df_Temp.insert(position, 'Duration', pd.to_datetime(df_Temp[fieldRow['Field_Label']], errors = 'coerce') - pd.to_datetime(df_Temp[fieldRow['Calc_Field']], errors = 'coerce'))
df_Temp['Duration'] = df_Temp['Duration'].dt.days
'''
1
  • Can you add some sample data and expected ouput? Commented Jun 27, 2022 at 6:26

1 Answer 1

0

Use f-strings for new column, Duration column is not created, because added dt.days for subtracted Series:

s = (pd.to_datetime(df_Temp[fieldRow['Field_Label']], errors = 'coerce') -
     pd.to_datetime(df_Temp[fieldRow['Calc_Field']], errors = 'coerce')).dt.days

df_Temp.insert(position, f"{fieldRow['Field_Label'} Duration", s)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.