我试图将poloar
转换为x-y
小组:
df['x']=df.apply(lambda x: x['speed'] * math.cos(math.radians(x['degree'])),axis=1)
df['y']=df.apply(lambda x: x['speed'] * math.sin(math.radians(x['degree'])),axis=1)
df.head()
问题是x
太长了,我怎样才能缩短它?
我在How to turn a float number like 293.4662543 into 293.47 in python?找到了,我可以"%.2f" % 1.2399
,但如果这是一个好方法吗?
答案 0 :(得分:1)
实际上,在这种情况下,np.round运行良好
> from pandas import DataFrame
> import numpy as np
> a = DataFrame(np.random.normal(size=10).reshape((5,2)))
0 1
0 -1.444689 -0.991011
1 1.054962 -0.288084
2 -0.700032 -0.604181
3 0.693142 2.281788
4 -1.647281 -1.309406
> np.round(a,2)
0 1
0 -1.44 -0.99
1 1.05 -0.29
2 -0.70 -0.60
3 0.69 2.28
4 -1.65 -1.31
您还可以通过简单地使用舍入值覆盖来对单个列进行舍入:
> a[1] = np.round(a[1],3)
> a
0 1
0 0.028320 -1.104
1 -0.121453 -0.179
2 -1.906779 -0.347
3 0.234835 -0.522
4 -0.309782 0.129