使用“符号”数字填充DataFrame

时间:2016-05-22 17:35:20

标签: python pandas

我有一个充满浮动(正面和负面)和一些NaN的DataFrame。 我想用它的符号替换每个浮点数:

if it's NaN -> it remains Nan
if positive -> replace with 1
if negative -> replace with -1
if zero -> leave it as 0

有任何建议可以进行大规模替换吗?

提前谢谢

3 个答案:

答案 0 :(得分:19)

您可以使用np.sign

<div class="example"><span class="text">BIG TEXT 1</span>
</div>
<div class="example"><span class="text">BIG TEXT 2</span>
</div>
<div class="example"><span class="text">BIG TEXT 3</span>
</div>

要应用于所有列,您可以直接传递数据帧:

df
Out[100]: 
     A
0 -4.0
1  2.0
2  NaN
3  0.0

import numpy as np
np.sign(df["A"])

Out[101]: 
0   -1.0
1    1.0
2    NaN
3    0.0
Name: A, dtype: float64

答案 1 :(得分:6)

您可以使用boolean indexing

df
Out[121]: 
          0         1         2         3
0 -2.932447 -1.686652       NaN -0.908441
1  1.254436  0.000000  0.072242  0.796944
2  2.626737  0.169639 -1.457195  1.169238
3  0.000000 -1.174251  0.660111  1.115518
4 -1.998091 -0.125095  0.000000 -0.506782

np.sign(df)
Out[122]: 
     0    1    2    3
0 -1.0 -1.0  NaN -1.0
1  1.0  0.0  1.0  1.0
2  1.0  1.0 -1.0  1.0
3  0.0 -1.0  1.0  1.0
4 -1.0 -1.0  0.0 -1.0
import pandas as pd
import numpy as np

df = pd.DataFrame({'A':[-1,3,0,5],
                   'B':[4,5,6,5],
                   'C':[8,-9,np.nan,7]})

print (df)
   A  B    C
0 -1  4  8.0
1  3  5 -9.0
2  0  6  NaN
3  5  5  7.0

答案 2 :(得分:2)

代码 -

import pandas as pd


df = pd.DataFrame({'x' : [-5.3, 2.5, 0, float('nan')]})

df['x'] = df['x'].apply(func = lambda x : x if not x else x // abs(x))

print(df)

输出 -

    x
0  -1
1   1
2   0
3 NaN