Filling a DataFrame with “sign” numbers

后端 未结 3 1768
小蘑菇
小蘑菇 2021-02-14 13:13

I have a DataFrame full of floats (positive and negative) and some NaN. I\'d like to replace every single float number with its sign:

if it\'s NaN -> it remai         


        
相关标签:
3条回答
  • 2021-02-14 13:22

    You can use boolean indexing:

    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
    
    print (df > 0)
           A     B      C
    0  False  True   True
    1   True  True  False
    2  False  True  False
    3   True  True   True
    
    print (df < 0)
           A      B      C
    0   True  False  False
    1  False  False   True
    2  False  False  False
    3  False  False  False
    
    df[df > 0] = 1
    df[df < 0] = -1
    
    print (df)
       A  B    C
    0 -1  1  1.0
    1  1  1 -1.0
    2  0  1  NaN
    3  1  1  1.0
    
    0 讨论(0)
  • 2021-02-14 13:38

    Code -

    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)
    

    Output -

        x
    0  -1
    1   1
    2   0
    3 NaN
    
    0 讨论(0)
  • 2021-02-14 13:40

    You can use np.sign:

    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
    

    In order to apply to all columns, you can directly pass the dataframe:

    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
    
    0 讨论(0)
提交回复
热议问题