[英]How to replace a float value with NaN in pandas?
I'm aware about the replace function in pandas: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html 我知道熊猫中的替换功能: https : //pandas.pydata.org/pandas-docs/stable/generation/pandas.DataFrame.replace.html
But I've done this simple test and it is not working as expected when I try to replace a float value: 但是我已经完成了这个简单的测试,当我尝试替换浮点值时,它无法按预期工作:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
print(df.head(n=1))
A B C D
0 1.437202 1.919894 -1.40674 -0.316737
df = df.replace(1.437202, np.nan)
print(df.head(n=1))
A B C D
0 1.437202 1.919894 -1.40674 -0.316737
As you see the [[0],[0]] has no change...any idea about what this could be due to? 如您所见,[[0],[0]]没变……关于这可能是什么的任何想法?
Problem is float precision, so use function numpy.isclose
with mask
: 问题是浮点精度,因此使用带mask
numpy.isclose
函数:
np.random.seed(123)
df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
print(df.head(n=1))
A B C D
0 -1.085631 0.997345 0.282978 -1.506295
df = df.mask(np.isclose(df.values, 0.997345))
Or use numpy.where
: 或使用numpy.where
:
arr = np.where(np.isclose(df.values, 0.997345), np.nan, df.values)
df = pd.DataFrame(arr, index=df.index, columns=df.columns)
print(df.head(n=1))
A B C D
0 -1.085631 NaN 0.282978 -1.506295
EDIT: You can also get only numeric columns by select_dtypes
for filtering by subset with []
: 编辑:您还可以通过select_dtypes
仅获取数字列,以使用[]
进行子集过滤:
np.random.seed(123)
df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD')).assign(E='a')
cols = df.select_dtypes(np.number).columns
df[cols] = df[cols].mask(np.isclose(df[cols].values, 0.997345))
print(df.head(n=1))
A B C D E
0 -1.085631 NaN 0.282978 -1.506295 a
Just a another trick for specific indices : 特定索引的另一招:
>>> print(df.head(n=1))
A B C D
0 -0.042839 1.701118 0.064779 1.513046
>>> df['A'][0] = np.nan
>>> print(df.head(n=1))
A B C D
0 NaN 1.701118 0.064779 1.513046
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.