我正在尝试在具有2列的数据框中将100
替换为NaN
,但是由于某种原因,它仅替换了第一列的前3行。可能是什么原因?
这是我的代码和输出:
print('Before', df, type(df))
df = df.replace(100, np.nan)
print('After', df)
Before A B
2020-09-17 00:00:00+00:00 100.000000 NaN
2020-09-18 00:00:00+00:00 100.000000 NaN
2020-09-18 01:00:00+00:00 100.000000 NaN
2020-09-18 02:00:00+00:00 100.000000 NaN
2020-09-18 03:00:00+00:00 100.000000 NaN
... ... ...
2020-10-03 04:00:00+00:00 99.916528 96.116235
2020-10-03 05:00:00+00:00 99.933531 95.525355
2020-10-03 06:00:00+00:00 99.942923 95.756569
2020-10-03 07:00:00+00:00 99.940998 95.721852
2020-10-03 08:00:00+00:00 99.941141 95.099550
[370 rows x 2 columns]
<class 'pandas.core.frame.DataFrame'>
After A B
2020-09-17 00:00:00+00:00 NaN NaN
2020-09-18 00:00:00+00:00 NaN NaN
2020-09-18 01:00:00+00:00 NaN NaN
2020-09-18 02:00:00+00:00 100.000000 NaN
2020-09-18 03:00:00+00:00 100.000000 NaN
... ... ...
2020-10-03 04:00:00+00:00 99.916528 96.116235
2020-10-03 05:00:00+00:00 99.933531 95.525355
2020-10-03 06:00:00+00:00 99.942923 95.756569
2020-10-03 07:00:00+00:00 99.940998 95.721852
2020-10-03 08:00:00+00:00 99.941141 95.099550
当我打印df['A']
时,它说dtype是float64
,所以我不知道如何解决此问题。而且,遍历列也不会得到预期的结果。
for col in df:
df[col] = df[col].replace(100, np.nan)
答案 0 :(得分:2)
未替换的值当然是恰好不是100的那些值。
熊猫的浮点数默认表示形式是小数点后6位。尝试将其扩展(例如扩展到20位数字):
pd.set_option("display.float_format", "{:.20f}".format)
在显示数据框以查看那些非精确值之前。