0

I am currently writing a function in pandas to try to check rows in a column to see if they are not null. If they are not null, I want something to be outputed to a new column and for this case it would be 'Financing'. Basically if a row has a value for loan funded date, I want the phrase Financing to be printed a new column called Payment Type.

def typepayment(x):
    if x['Loan Funded Date'] != np.nan:
        x['Payment Type'] = 'Financing'
    return x
df2 = df1.apply(typepayment, axis = 1)
df2

The output for the code above still outputs Financing in Payment Type for rows that are null in Loan Funded Date. What is causing this problem?

1 Answer 1

1

x['Loan Funded Date'] != np.nan will be True if x['Loan Funded Date'] is null (NaN is not equal with itself). In this case, x['Payment Type'] will be set to 'Financing'. As a alternative, you can use

m = df1['Loan Funded Date'].notna()
df1.loc[m, 'Payment Type'] = 'Financing'
# or
df1['Payment Type'] = df1['Payment Type'].mask(m, 'Financing')
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.