Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If Elif statement isn't working properly >> only reads first elif and jumps the other ones after it – python

So I created this function to apply to a bunch of dataframes that I have inside a dictionary. The thing is, when it comes to the elif part he only does the first statement that he reads.

So for example if I don’t have the column 'claimant moderated resultaction' he will create it as I tell him to do but he won’t create the 'claimant moderated resultselectedPolicyTitle' because it comes next in the iteration. Same logic for the rest of columns.

cols = ['Moderation Queue Title', 'Object ID', 'Moderator', 'Appeal Status', 'Tag', 'claimant moderated resultaction', 'claimant moderated resultselectedPolicyTitle', 'respondent Sampling Informationaction', 'respondent Sampling InformationselectedPolicyTitle', 'Sampling Time']

def transformations(df):
    for k, v in df.items():
        df[k] = df[k].dropna(axis=1, how='all')
        df[k].columns = np.where(df[k].loc[1] == df[k].loc[0], df[k].loc[0], df[k].loc[0]+df[k].loc[1])
        df[k] = df[k].drop([df[k].index[0], df[k].index[1]])
        if 'Moderation Queue Title' not in df[k]:
            df[k]['Moderation Queue Title'] = ""
        elif 'Object ID' not in df[k]:
            df[k]['Object ID'] = ""
        elif 'Moderator' not in df[k]:
            df[k]['Moderator'] = ""
        elif 'Appeal Status' not in df[k]:
            df[k]['Appeal Status'] = ""
        elif 'Tag' not in df[k]:
            df[k]['Tag'] = ""
        elif 'claimant moderated resultaction' not in df[k]:
            df[k]['claimant moderated resultaction'] = ""
        elif 'claimant moderated resultselectedPolicyTitle' not in df[k]:
            df[k]['claimant moderated resultselectedPolicyTitle'] = ""
        elif 'respondent Sampling Informationaction' not in df[k]:
            df[k]['respondent Sampling Informationaction'] = ""
        elif 'respondent Sampling InformationselectedPolicyTitle' not in df[k]:
            df[k]['respondent Sampling InformationselectedPolicyTitle'] = ""
        elif 'Sampling Time' not in df[k]:
            df[k]['Sampling Time'] = ""
        elif set(cols).issubset(df[k].columns):
            df[k] = df[k][cols]
        else:
            df[k] = df[k][cols] 
 

I am probably doing something wrong here, can you please point out me what I am missing or provide an alternative approach?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you very much!!

>Solution :

IF, ELIF ans ELSE syntax:

IF statement will enter if its true. If you have 30 if’s, it will test all of them. If this "if" is TRUE it will enter.

ELIF statement will enter the first and ignore all others. If you have 30 ELIF it will enter only in the first one that is TRUE.

ELSE statement will enter if all other IF’s and ELIF’s are FALSE.

So… if you want to test them all. Change ELIF for IF.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading