jeudi 19 juillet 2018

Multiple IF statements comparing 2 dataframes and populating dataframe based on result

I have 2 dataframes, one with a list of systems and versions and another with a list of all the systems/versions and whether they are obsolete, and I'm struggling to perform some sort of multi-if statement (including comparators) and populate the data in the first df with the data from the second.

The dataframes are;

>>> system_df
    ID    SIS_TYPE   EDITION   VERSION
0   SYS1      AMA     first       2
1   SYS2      DIR     first       10.3
2   SYS3      DIR     second      9
3   SYS4      ORA     first       7




>>> obsolete_df
    SIS_TYPE   EDITION   VERSION    OBSOLETE
0      AMA     first      2          YES
1      DIR     first      10         NO
2      DIR       *        9.1        NO
3      ORA     third      7          YES
4      ORA       *        9          NO
4      ORA       *        8          YES

What I'm trying to do is the following;

First I need to lookup the SIS_TYPE in obsolet_df, then I want to check the EDITION for a match, but if there is not match in EDITION it should match with the asterisk. After finding a match with both these columns I then want to check the VERSION column for one of these three things;

  • a direct match
  • a partial match using a regex to compare the first number (9.1 = 9)
  • if none of the above then check the value of the next highest version (checking 9 against 10, 11 etc.)

Final step would be to add the OBSOLETE value into the system_df if all of these conditions are satisfied.

So the desired result would be something like;

>>> final_df
    ID    SIS_TYPE   EDITION   VERSION    OBSOLETE
0   SYS1      AMA     first      2          YES
1   SYS2      DIR     first      10         NO
2   SYS3      DIR       *        9.1        NO
3   SYS4      ORA     third      8          YES
4   SYS5      ORA       *        *          YES

I've tried many combinations of .loc, merge and join but I cannot get the dataframe how I need it.

Aucun commentaire:

Enregistrer un commentaire