简体   繁体   English

在 python 3.8 中将访问逻辑转换为 Pandas 的问题

[英]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.我目前正在使用 Pandas 将一些项目从访问数据库转移到 python 3.8。 I've been running into an issue with the logic for one of the queries.我遇到了其中一个查询的逻辑问题。
the MSACCESS query is as follows: MSACCESS 查询如下:

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.在 python 我认为这将是等效的。

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.不幸的是,我发现在 Access 中有 CLCL_NTWK_IND 或 PR_PRPR_STS 为空白的条目,它们保持 FlagExclude = 0。在 Pandas/python 代码中,它设置了 '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.我知道.loc 在其他运算符之前评估 & 所以我尝试在代码中使用各种不同的 () 集以及在.isin 语句中包含 '' 但它继续将空白条目评估为 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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