你需要 numpy.sign
df['new'] = np.sign(df['col'])
Run Code Online (Sandbox Code Playgroud)
样品:
df = pd.DataFrame({ 'col':[-1,3,-5,7,1,0]})
df['new'] = np.sign(df['col'])
print (df)
col new
0 -1 -1
1 3 1
2 -5 -1
3 7 1
4 1 1
5 0 0
Run Code Online (Sandbox Code Playgroud)