0

I have one dataframe

df1

I want to add a column in this datafram as 'VAR', the values in this new column should be updated with "L" if the difference between 'END' and 'START' column is greater than or equal to 365, otherwise it should be "S"

Final output should be

df1

ManojK
  • 1,570
  • 2
  • 9
  • 17

2 Answers2

0

I know this answer is not in Pandas, but the tag Excel gave out that you're also using the to_excel, so: you can simply get your result using Excel's functions. Assuming the first "End" value is in column C4, and the "Start" value is B4, run this function: =IF(C4-B4>=365,"L","S") and you will get the desired outcome. If you want to do it in Pandas alone, have a look at this answer, which you could add an if check for the column.

Andre
  • 47
  • 7
0

Once you read data in pandas dataframe, then convert the Start and End column to pandas.to_datetime:

df1 = pd.DataFrame({'Start':['21-01-2015','28-02-2019','07-04-2017','01-01-2019'],
                   'End':['25-11-2021','02-01-2020','10-02-2020','31-12-2019']})

df1['Start'] = pd.to_datetime(df1['Start'])
df1['End'] = pd.to_datetime(df1['End'])

#Take difference between 'End' and 'Start'

df1['diff'] = (df1['End'] - df1['Start']).dt.days

#Then use lambda function to apply the condition:

df1['Var'] = df1['diff'].apply(lambda x: 'L' if x > 364 else 'S')

print(df1)

       Start        End  diff Var
0 2015-01-21 2021-11-25  2500   L
1 2019-02-28 2020-02-01   338   S
2 2017-07-04 2020-10-02  1186   L
3 2019-01-01 2019-12-31   364   S

#Then drop the temporary diff column

df1 = df1.drop(['diff'], axis = 1)
ManojK
  • 1,570
  • 2
  • 9
  • 17