Python中pandas如何将某一列大于0的数字全部置为1
date rtn 0 2016-01-01 0.101536 1 2016-01-02 0.055109 2 2016-01-03 -0.163625 3 2016-01-04 0.086756 4 2016-01-05 0.035907 5 2016-01-06 0.047619 6 2016-01-07 0.018717 7 2016-01-08 -0.036290
这样子的 df
大佬帮帮忙,实在搞不出来了
Python中pandas如何将某一列大于0的数字全部置为1
7 回复
df[‘rtn’] = df[‘rtn’].apply(lambda x: 1 if x>0 else x)
df.loc[df[‘rtn’]>0,‘rtn’]=1
直接改为 true,false 矩阵
x = x**0
写错,请忽略。。。
df[‘rtn’] = np.where(df[‘rtn’]>0, 1, df[‘rtn’])


