简体   繁体   中英

Issue translating Access Logic to Pandas in python 3.8

I'm currently working to transfer some projects from access databases to python 3.8 using Pandas. I've been running into an issue with the logic for one of the queries.
the MSACCESS query is as follows:

UPDATE tblStagingMaternity AS tblStaging SET tblStaging.FlagExclude = 4
WHERE tblStaging.FlagExclude=0 AND NOT (tblStaging.CLCL_NTWK_IND='I' OR tblStaging.CLCL_NTWK_IND='P' OR tblStaging.PR_PRPR_STS='PA');

In python I thought this would be equivalent.

Stage_Maternity_DF.loc[((Stage_Maternity_DF['FlagExclude'] == 0) &
                      (~((Stage_Maternity_DF['CLCL_NTWK_IND'].isin(['I','P'])) |
                        (Stage_Maternity_DF['PR_PRPR_STS'].isin(['PA']))))),
                      'FlagExclude'] = 4

Unfortunately I've found that in Access there are entries where CLCL_NTWK_IND or PR_PRPR_STS are blank they stay FlagExclude = 0. In the Pandas/python code it sets the 'FlagExclude' =4. I know that.loc evaluates & before other operators so I've tried using various different sets of () with the code as well as including '' in the.isin statements but it continue to evaluate the blank entries to 4.

I suggest explicitly treating blanks before filtering.

For example, consider the following as a pre-processing step before you filter (may need to update for your data types/values)

df.fillna(value = 0, inplace = True)

or

df.replace('Missing', np.nan, inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM