1

if a1_status, b1_status, c1_status, d1_status equal "Active" and a1, b1, c1, d1 equals "Loan" then output column "Loan_active" contains count of "Active" row wise. input dataframe looks like this.

enter image description here

output dataframe :

enter image description here

use below code to create same dataframe mentioned above in image in pandas .

import pandas as pd
df = pd.DataFrame({'a1':['Loan','Loan','Loan'],
            'a1_status' : ['active','closed','active'] ,
            'b1' : ['Loan','Loan','Loan'],
            'b1_status' : ['active','active','active'] ,
            'c1' : ['Credit','Credit','Credit'],
            'c1_status' : ['closed','closed','closed'] ,
            'd1' : ['Loan','Loan','Loan'],
            'd1_status' : ['closed','closed','active'] ,
})
print(df)
3
  • 1
    What is the output columns name, d1_status or Loan_acitve which shows in your desire table ? And the output column in your desire table doesn't fit what you want in your question. Values in c1 only contain Credit, which doesn't meet the given condition for generating values for the new column. Commented Feb 25, 2022 at 2:34
  • if c1 column doesn't contain "Loan" value then i have to ignore that. and "Loan_acitve" is the output column name Commented Feb 25, 2022 at 2:37
  • did you attempt to solve it? kindly share your code for the attempt(s) Commented Feb 25, 2022 at 2:38

2 Answers 2

4

Let us do shift

df['new'] = (df.eq('active') & df.shift(axis=1).eq('Loan')).sum(axis=1)
Out[349]: 
0    2
1    1
2    3
dtype: int64
Sign up to request clarification or add additional context in comments.

1 Comment

This is very elegant. Of course, it assumes that the "loan" column will be just left to "status" columns, but it can easily be worked around by sorting the columns first.
1

This solution is not as elegant as the other, but it explictly select the columns that includes ONLY loans, then check their status. But this also assumes that the status columns will be named loan_status.

loan = df.columns[df.isin(["Loan"]).all()]
df['loan_active'] = df[loan+"_status"].eq('active').sum(axis=1)

or if you prefer one liners;

df['loan_active'] = df[df.columns[df.isin(["Loan"]).all()]+"_status"].eq('active').sum(axis=1)

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.