Python 根据阈值替换串联的值
我有一个熊猫系列,如果值<3,我想用0替换值,如果值>=3,我想用1替换值Python 根据阈值替换串联的值,python,pandas,series,Python,Pandas,Series,我有一个熊猫系列,如果值=3,我想用1替换值 se = pandas.Series([1,2,3,4,5,6]) se[se<3]=0 se[se>=3]=1 se=pandas.系列([1,2,3,4,5,6]) se[se=3]=1 有没有更好的/pythonic的方法来实现这一点?在我看来,这里是对整数的最佳/快速转换布尔掩码: se = (se >= 3).astype(int) 或使用,但必须使用构造函数,因为返回的numpy数组: se = pd.Series
se = pandas.Series([1,2,3,4,5,6])
se[se<3]=0
se[se>=3]=1
se=pandas.系列([1,2,3,4,5,6])
se[se=3]=1
有没有更好的/pythonic的方法来实现这一点?在我看来,这里是对
整数的最佳/快速转换布尔掩码:
se = (se >= 3).astype(int)
或使用,但必须使用构造函数,因为返回的numpy数组:
se = pd.Series(np.where(se < 3, 0, 1), index=se.index)
print (se)
0 0
1 0
2 1
3 1
4 1
5 1
dtype: int32
se=pd.Series(np.where(se<3,0,1),index=se.index)
印刷品(se)
0 0
1 0
2 1
3 1
4 1
5 1
数据类型:int32
输出:
0 0
1 0
2 1
3 1
4 1
5 1
dtype: int64
我认为你的实施一切顺利
0 0
1 0
2 1
3 1
4 1
5 1
dtype: int64