pandas :添加带有其他列计算的列 [英] Pandas: Adding column with calculations from other columns
本文介绍了 pandas :添加带有其他列计算的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有测量结果的csv:
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:51:07:609 | 3 | 4
,我想添加另一列,其总和为x ^ 2 + y ^ 2的平方根, z = sqrt(x ^ 2 + y ^ 2)
and I want to add another column with the square root of the sum of x^2+y^2, z=sqrt(x^2+y^2)
像这样:
YY-MO-DD HH-MI-SS_SSS | x | y | z
2015-12-07 20:51:06:608 | 2 | 4 | 4.472
2015-12-07 20:51:07:609 | 3 | 4 | 5
有什么想法吗?
谢谢!
推荐答案
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
您还可以按sum
逐行显示np.square
的结果并调用np.sqrt
:
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
这篇关于 pandas :添加带有其他列计算的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文