将 strip() 映射到 pandas 数据框中的字符串不会更改 NaN 条目,但仍然声称它们不同?

问题描述 投票:0回答:1

我有一个数据框,其中有非常不同类型的条目(文本、整数、浮点数、时间等),我试图从文本条目中删除前导和尾随空格,以便我的其他代码可以按预期工作。但是,我的代码似乎不起作用。

这是我正在尝试做的一个简单示例:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(np.array(([np.nan, 2, 3], [4, 5, 6])), columns=["one", "two", "three"])
print(df1)
print("")
df2 = df1.map(lambda x: x.strip() if isinstance(x, str) else x)
print(df2)
print("")
print(df1==df2)
print("")
cell1 = df2.at[0, "one"]
cell2 = df1.at[0, "one"]
print(cell1, type(cell1))
print(cell2, type(cell2))
print(cell1==cell2)

当我运行此代码时,输出是:

   one  two  three
0  NaN  2.0    3.0
1  4.0  5.0    6.0

   one  two  three
0  NaN  2.0    3.0
1  4.0  5.0    6.0

     one   two  three
0  False  True   True
1   True  True   True

nan <class 'numpy.float64'>
nan <class 'numpy.float64'>
False

如您所见,

df1
df2
具有完全相同的整体(NaN),但代码块
print(cell1==cell2)
声称这些单元格不同。

这里发生了什么?

python-3.x pandas lambda mapping
1个回答
0
投票

这就是浮动的工作原理,你不能直接比较

NaN
s。

使用

Dataframe.equals
比较数据帧:

df1 = pd.DataFrame(
    np.array(([np.nan, 2, 3], [4, 5, 6])), columns=["one", "two", "three"]
)

df2 = df1.map(lambda x: x.strip() if isinstance(x, str) else x)

print(df1.equals(df2))

打印:

True
© www.soinside.com 2019 - 2024. All rights reserved.