我有一个以下numpy数组:
geom= [[ 6. 0.2 -1.6 -1.3915 ]
[ 6. 1.40507435 -1.6 -0.69575 ]
[ 6. 1.40507435 -1.6 0.69575 ]
[ 6. 0.2 -1.6 1.3915 ]
[ 6. -1.00507435 -1.6 0.69575 ]
[ 6. -1.00507435 -1.6 -0.69575 ]]
当我将其保存到文件中时:
np.savetxt(g, geom, fmt ='%f6', delimiter=' ', newline='\n', header='', footer='', comments='# ')
我的第一列获得格式'6.0000008'。随后我想修改该文件以获得6.0或6.有没有简单的解决方案呢? 我试图将字符串转换为int,但我收到错误消息:
ValueError: invalid literal for int() with base 10: '6.0000008'
答案 0 :(得分:0)
假设你想保留其他花车,你就不会在NumPy中找到一个简单的解决方案。当然,你可以使用structured arrays来实现你想要的东西,但这比使用Pandas(正如@jakevdp所提到的)要多得多。
您可以将NumPy数组导入Pandas DataFrame,进行所需的更改,然后将数据输出为任何Pandas可接受的格式。
例如,写一个csv:
df = pd.DataFrame(geom)
df[0] = df[0].astype(int)
df.to_csv(g, index=False)