python - pd.Series 替换第一次出现

标签 python pandas dataframe series

当尝试用 np.NaN 替换系列的最小值时,我得到了“多个”替换。

我尝试使用series.replace方法,但它会导致替换所有出现的最小值。

举个例子:

series = pd.Series([0,0,1,1])

#when calling the replace:
series = series.apply(lambda x: x.replace(min(x),np.NaN))

#the output is:
0   NaN
1   NaN
2   1
3   1

#and the desired output is:
0   NaN
1   0
2   1
3   1

所以本质上,我怎样才能只替换第一次出现的重复最小值?

最佳答案

loc+idxmin

idxmin 返回系列最小值第一次出现的索引。

sss.loc[sss.idxmin()] = np.nan

关于python - pd.Series 替换第一次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58405533/

相关文章:

python - 通过 OBEX 发送文件(浅蓝色/OBEXFTP)

python - 按顺序打印字符串

python - 如何匹配 pandas 数据帧同一列中的两个值

python - 根据字典重命名 Pandas 中的列

python - 将新值与以前的值进行比较,如果在 Excel 中不相同则标记(使用 Python)

python - 如何删除重复条目

r - 如何使用变量名提取数据框的列索引?

python - 如何在没有省略号的情况下打印所有 eli5.explain_weights() 结果?

python - Flask App目录和权限的设置?

python - 将数据框行转换为 Python 集