python - 尝试在比较之前将 NaT、NaN 转换为字符串并在 Pandas 中失败
问题描述
为了避免任何 NaT、NaN 和 None 比较问题,我尝试在进行比较之前转换为字符串值“ NULL ”。
if frames_equal == False:
print(file_name, " value by value check for differences:")
source_columns = df.columns;
print(file_name, " columns:")
print(source_columns);
for source_index, source_row in df.iterrows():
for source_col in source_columns:
source_value = source_row[source_col];
target_value = df_file.loc[source_index, source_col];
if pd.isna(source_value) or pd.isnull(source_value):
source_value = '__NULL__';
elif pd.isna(target_value) or pd.isnull(target_value):
target_value = '__NULL__';
if source_value != target_value:
values_equal = False;
print("~" * 50);
print(file_name, " value differences in column ", source_col);
print("MISMATCH AT INDEX: ", source_index)
print("REGISTRATION_UID: ", source_row["REGISTRATION_UID"])
print("Column: ", source_col);
print("Source Value: ", source_value);
print("Source Type: ", type(source_value));
print("Target Value: ", target_value);
print("Target Type: ", type(target_value));
print("~" * 50)
在比较之前,我通过在源值和目标值上使用 pd.isna() 或 pd.isnull() 检查源值或目标值是否为空。
但是,我的输出中仍然有不等式测试。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020_07_27__lu_volume.csv value differences in column LU_INSERT_YEAR
MISMATCH AT INDEX: 23740
REGISTRATION_UID: ZOMI-00041736
Column: LU_INSERT_YEAR
Source Value: __NULL__
Source Type: <class 'str'>
Target Value: nan
Target Type: <class 'numpy.float64'>
这意味着在比较之前我的 nan 值没有被拾取并转换为“ NULL ”字符串?
解决方案
推荐阅读
- angular - 在加载第一个组件之前在 Angular 中显示预加载器
- java - 使用改造从 api 获取响应以在 recycleview 中获取响应时出错
- git - 在 Master 中创建新的分支以保持未暂存的更改
- python - Visual Studio 2017 Python 调试器问题
- python - 计算列表中的字符
- c++ - Declare C++ array type with dynamic size works fine in Linux, but not Windows
- python - Telegram Bot 获取用户电话号码
- javascript - How can Babel be used without bundler, but with a dev-experience similar to webpack?
- powershell - For PowerShell cmdlets, can I always pass a script block to a string parameter?
- python-3.x - 如何从 youtube 频道获取所有视频