当尝试用 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/