Pandas: Adding column with calculations from other columns

后端 未结 1 1517
南旧
南旧 2021-01-21 00:02

I have a csv with measurements:

YY-MO-DD HH-MI-SS_SSS    |        x          |          y
2015-12-07 20:51:06:608  |        2          |          4
2015-12-07 20         


        
相关标签:
1条回答
  • 2021-01-21 00:26

    Use np.sqrt on the result of the squares:

    In [10]:
    df['z'] = np.sqrt(df['x']**2 + df['y']**2)
    df
    
    Out[10]:
       x  y         z
    0  2  4  4.472136
    1  3  4  5.000000
    

    You can also sum row-wise the result of np.square and call np.sqrt:

    In [13]:
    df['z'] = np.sqrt(np.square(df[['x','y']]).sum(axis=1))
    df
    
    Out[13]:
       x  y         z
    0  2  4  4.472136
    1  3  4  5.000000
    
    0 讨论(0)
提交回复
热议问题