Python 将数据帧列中低于某个阈值的值替换为NaN

Python 将数据帧列中低于某个阈值的值替换为NaN,python,pandas,dataframe,nan,nonetype,Python,Pandas,Dataframe,Nan,Nonetype,假设我有以下示例数据帧: 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

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

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\u indexer,col\u indexer]=值来修改数据帧


下面的代码对您有用吗?我使用.loc[row\u indexer,col\u indexer]=值来修改数据帧

请尝试:

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

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

np.在哪里


你能显示你的样本数据和预期输出吗?df['A']=df['A']>截止点在哪里?你能显示你的样本数据和预期输出吗?df['A']=df['A']。df['A']>截止点在哪里?
df['A'] = df2['A'].apply(lambda x: x if x > cutoff else None)
df['A'] = np.where(df['A']<=cutoff , np.nan, df['A'])