Python pandas或numpy数组数据元素格式
环境:Python 3.7.6,libsPython pandas或numpy数组数据元素格式,python,pandas,numpy,Python,Pandas,Numpy,环境:Python 3.7.6,libsnumpy==1.18.2和pandas==1.0.3 将numpy导入为np 作为pd进口熊猫 np.set\u打印选项(suppress=True) pd.set_选项('display.float_格式',lambda x:'%0.2f'%x〕) #不起作用? 数据=pd.read\u csv(“test.csv”) """ #下面是test.csv示例数据 以,价格 1587690840,15.25 1587690900,15.24 1587690
numpy==1.18.2
和pandas==1.0.3
将numpy导入为np
作为pd进口熊猫
np.set\u打印选项(suppress=True)
pd.set_选项('display.float_格式',lambda x:'%0.2f'%x〕)
#不起作用?
数据=pd.read\u csv(“test.csv”)
"""
#下面是test.csv示例数据
以,价格
1587690840,15.25
1587690900,15.24
1587690960,15.23
---
"""
x=np.asarray(数据)
打印(x)
"""
#结果:
[[1.58769084e+09 1.52500000e+01]
[1.58769090e+09 1.52400000e+01]
[1.58769096e+09 1.52300000e+01]]
"""
我希望第一个元素转换为不带科学符号的int32,第二个元素转换为float32%.2f
如何使用x
结果修改代码,如下所示:
[[1587690840 15.25]
[1587690900 15.24]
[1587690960 15.23]]
我认为使用
set\u printoptions
方法的formatter
选项是不可能的。使用apply\u over\u axes
后不能执行此操作吗?传统的numpy数组无法存储多个类型,如果您希望打开多个数据类型,请参阅
array_f=np.zeros(3,dtype={'names':('integers','floats'),
“格式”:(np.int32,np.float32)})
数组_f['integers']=x[:,0]
数组_f['floats']=x[:,1]
数组
#阵列([(1587690840,15.25),(158769000,15.24),(158769060,15.23)],
#数据类型=[('integers','您的数据作为结构化数据类型:
In [166]: txt = """at,price
...: 1587690840,15.25
...: 1587690900,15.24
...: 1587690960,15.23"""
In [167]: data = np.genfromtxt(txt.splitlines(), delimiter=',', names=True, dtype=None, encoding=None)
In [168]: data
Out[168]:
array([(1587690840, 15.25), (1587690900, 15.24), (1587690960, 15.23)],
dtype=[('at', '<i8'), ('price', '<f8')])
我没有做过太多的设置打印选项,但是看起来浮动这么大(1.58e9)时,suppress=True
似乎没有效果。这两列分别显示:
In [176]: data[:,0]
Out[176]: array([1.58769084e+09, 1.58769090e+09, 1.58769096e+09])
In [177]: data[:,1]
Out[177]: array([15.25, 15.24, 15.23])
和转换为int的大浮点:
In [178]: data[:,0].astype(int)
Out[178]: array([1587690840, 1587690900, 1587690960])
你的pd.read\u csv
产生了什么
In [189]: pd.DataFrame(data, dtype=None)
Out[189]:
0 1
0 1.587691e+09 15.25
1 1.587691e+09 15.24
2 1.587691e+09 15.23
In [190]: pd.DataFrame(Out[168], dtype=None)
Out[190]:
at price
0 1587690840 15.25
1 1587690900 15.24
2 1587690960 15.23
将数据帧转换回阵列:
In [191]: Out[190].to_numpy()
Out[191]:
array([[1.58769084e+09, 1.52500000e+01],
[1.58769090e+09, 1.52400000e+01],
[1.58769096e+09, 1.52300000e+01]])
In [193]: Out[190].to_records(index=False)
Out[193]:
rec.array([(1587690840, 15.25), (1587690900, 15.24), (1587690960, 15.23)],
dtype=[('at', '<i8'), ('price', '<f8')])
numpy
选择了科学记数法(仅用于显示),因为它的值范围很广。整个数组具有float
dtype。感谢您的回答。让我了解有关熊猫的更多信息。
In [189]: pd.DataFrame(data, dtype=None)
Out[189]:
0 1
0 1.587691e+09 15.25
1 1.587691e+09 15.24
2 1.587691e+09 15.23
In [190]: pd.DataFrame(Out[168], dtype=None)
Out[190]:
at price
0 1587690840 15.25
1 1587690900 15.24
2 1587690960 15.23
In [191]: Out[190].to_numpy()
Out[191]:
array([[1.58769084e+09, 1.52500000e+01],
[1.58769090e+09, 1.52400000e+01],
[1.58769096e+09, 1.52300000e+01]])
In [193]: Out[190].to_records(index=False)
Out[193]:
rec.array([(1587690840, 15.25), (1587690900, 15.24), (1587690960, 15.23)],
dtype=[('at', '<i8'), ('price', '<f8')])
In [201]: with np.printoptions(suppress=True):
...: print(data/[100,1])
...:
[[15876908.4 15.25]
[15876909. 15.24]
[15876909.6 15.23]]