- We already have a column called LoanDurationDays. Still, if this column was not present, we could create this column by taking the difference between LoanStartDate and LoanEndDate.
To extract the month from the LoanStartDate column and create a new column, and to calculate the loan duration in days if the LoanDurationDays column was not present, we can use the following code:
1# Ensure 'LoanStartDate' and 'LoanEndDate' are in datetime format
2loan_data_cleaned['LoanStartDate'] = pd.to_datetime(loan_data_cleaned['LoanStartDate'])
3loan_data_cleaned['LoanEndDate'] = pd.to_datetime(loan_data_cleaned['LoanEndDate'])
4
5# Extract the month from 'LoanStartDate' and create a new column
6loan_data_cleaned['StartMonth'] = loan_data_cleaned['LoanStartDate'].dt.month
7
8# If 'LoanDurationDays' was not present, calculate it as the difference between 'LoanEndDate' and 'LoanStartDate'
9# Uncomment the following line if you want to create 'LoanDurationDays'
10
11# loan_data_cleaned['LoanPeriod'] = (loan_data_cleaned['LoanEndDate'] - loan_data_cleaned['LoanStartDate']).dt.days
12
13# Verify the changes
14loan_data_cleaned.head()
15
16
This code will first ensure that both LoanStartDate and LoanEndDate are in the correct datetime format. It then extracts the month from LoanStartDate and creates a new column StartMonth. If you need to create the LoanDurationDays column, you can uncomment the relevant line in the code; this will calculate the duration in days as the difference between LoanEndDate and LoanStartDate. The .dt accessor is used to access datetime properties of the columns.