【问题标题】:pandas or numpy array data elements formattingpandas 或 numpy 数组数据元素格式化
【发布时间】:2020-08-07 14:14:00
【问题描述】:

环境:Python 3.7.6 和库 numpy==1.18.2pandas==1.0.3

import numpy as np
import pandas as pd


np.set_printoptions(suppress=True)
pd.set_option('display.float_format', lambda x: '%.2f' % x)
# does not work ?

data = pd.read_csv("test.csv")

"""
# here is test.csv sample data
at,price
1587690840,15.25
1587690900,15.24
1587690960,15.23
---
"""
x = np.asarray(data)
print(x)

"""
# result:

[[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]]

【问题讨论】:

  • numpy 选择了科学记数法(仅用于显示),因为取值范围很广。整个数组有float dtype。

标签: python pandas numpy


【解决方案1】:

我认为set_printoptions 方法的formatter 选项不可能。用apply_over_axes 之后你不能这样做吗?

【讨论】:

    【解决方案2】:

    传统的 numpy 数组无法存储多种类型,如果您正在寻找有多种 dtype 的请参考structured arrays

    array_f = np.zeros(3, dtype={'names':('integers', 'floats'),
                          'formats':(np.int32, np.float32)})
    
    array_f['integers'] = x[:,0]
    array_f['floats'] = x[:,1]
    
    array_f
    
    # array([(1587690840, 15.25), (1587690900, 15.24), (1587690960, 15.23)],
    # dtype=[('integers', '<i4'), ('floats', '<f4')])
    

    但老实说,我认为 pandas 在这些情况下更有能力。

    【讨论】:

      【解决方案3】:

      您的数据作为结构化数据类型:

      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')])
      

      它有一个 int 字段和一个 float 字段。

      与浮动加载相同的东西

      In [170]: data = np.genfromtxt(txt.splitlines(), delimiter=',', skip_header=1, encoding=None)          
      In [171]: data                                                                                         
      Out[171]: 
      array([[1.58769084e+09, 1.52500000e+01],
             [1.58769090e+09, 1.52400000e+01],
             [1.58769096e+09, 1.52300000e+01]])
      

      set_printoptions 我工作得不多,但看起来suppress=True 对浮动这么大 (1.58e9) 没有影响。两列,分别显示:

      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_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')])
      

      如果最大数字较小,suppress 确实有效:

      In [201]: with np.printoptions(suppress=True): 
           ...:     print(data/[100,1]) 
           ...:                                                                                              
      [[15876908.4        15.25]
       [15876909.         15.24]
       [15876909.6        15.23]]
      

      【讨论】:

      • 感谢您的回答。让我了解更多关于熊猫的信息。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2017-11-07
      相关资源
      最近更新 更多