用 NaN 替换数据框列中低于特定阈值的值

Replace values in a dataframe column that are below a certain threshold with NaN

假设我有以下示例数据框:

df = pd.DataFrame({'A': [4, 0.2, 3, 0.5], 'B': ['red', 'white', 'blue', 'green']})

     A      B
0  4.0    red
1  0.2  white
2  3.0   blue
3  0.5  green

我正在尝试用 NaN 替换列中低于特定阈值的条目,如下所示:

     A      B
0  4.0    red
1  NaN  white
2  3.0   blue
3  NaN  green

这是我的尝试:

cutoff = 2
df['A'] = df['A'].apply(lambda x: [y if y > cutoff else None for y in x])

我收到的错误:

TypeError: 'float' object is not iterable

我哪里做错了?我认为它与 None 类型

有关

下面的代码对你有用吗?我使用 .loc[row_indexer,col_indexer] = value 修改数据框 (link to the documentation)

import pandas as pd
import numpy as np

df = pd.DataFrame({'A': [4, 0.2, 3, 0.5], 'B': ['red', 'white', 'blue', 'green']})
df.loc[df['A'] < 1, 'A'] = np.nan
print(df)

输出:

     A      B
0  4.0    red
1  NaN  white
2  3.0   blue
3  NaN  green

试试看:

df['A'] = df2['A'].apply(lambda x: x if x > cutoff else None)

np.where

df['A'] = np.where(df['A']<=cutoff , np.nan, df['A'])