'Creating function in pandas and applying it [closed]

I have this file. One column is named closing date, the second column is the expected closing date. If the closing date is less than or equal to the expected date then I have to create a column with Value on time. If it is greater than Late in that column. I have to create a function only in Pandas. When I run this code: delay=[]

for ctr in range(approved_crs_ext.shape[0]): approved_crs_ext=approved_crs['Credit Request: Credit Request Approved'][ctr]-approved_crs['Credit Request: Expected Approval Date'][ctr] if approved_crs_ext.days>0: delay.append('Late') else: delay.append('On time') approved_crs_ext['On time Check']=delay I get an error: File "", line 4, in approved_crs_ext=approved_crs['Credit Request: Credit Request Approved'][ctr]-approved_crs['Credit Request: Expected Approval Date'][ctr]

TypeError: unsupported operand type(s) for -: 'str' and 'str'



Solution 1:[1]

Use:

import pandas as pd
df = pd.DataFrame({'closing date': [5,1,10,80], 'expected closing':[3,2,11,75]})
df['new col'] = (df['closing date']<=df['expected closing']).replace(True, 'On time').replace(False, 'Late')

Output:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 keramat