【问题标题】:Convert a numpy matrix to a set of pandas Series将一个 numpy 矩阵转换为一组 pandas Series
【发布时间】:2021-07-22 10:03:12
【问题描述】:

问题:有没有一种快速的方法可以将 2D Numpy 矩阵转换为一组 Pandas Series?例如,一个 (100 x5) ndarray,到 5 个系列,每个系列 100 行。

背景:我需要使用随机生成的不同类型(浮点数、字符串等)的数据创建一个熊猫数据框。目前,对于浮点数,我创建了一个 numpy 矩阵,对于字符串,我创建了一个字符串数组。然后我将所有这些沿轴 = 1 组合成一个数据框。这不会保留每个单独列的数据类型。

为了保留数据类型,我打算使用 pandas 系列。由于创建多个浮点数系列可能比创建浮点数的 numpy 矩阵要慢,我想知道是否有办法将 numpy 矩阵转换为一组系列。

This 的问题与我的不同,它询问将 numpy 矩阵转换为单个系列。我需要多个系列。

【问题讨论】:

  • 您是否考虑过从矩阵中制作一个 DataFrame?
  • [pd.Series(arr[:,i]) for i in range(arr.shape[1])]arr 的每一列制作一个系列

标签: python pandas numpy


【解决方案1】:

您可以将每个数据类型的矩阵直接转换为dataframe,然后连接生成的dataframe。

float_df = pd.DataFrame(np.random.rand(500).reshape((-1,5)))
#           0         1         2         3         4
#0   0.561765  0.177957  0.279419  0.332973  0.967186
#1   0.761327  0.323747  0.707742  0.555475  0.680662
#..       ...       ...       ...       ...       ...
#98  0.741207  0.061200  0.142316  0.381168  0.591554
#99  0.417697  0.723469  0.730677  0.538261  0.281296
#
#[100 rows x 5 columns]

pd.concat([float_df, int_df, ...], axis=1)

【讨论】:

    【解决方案2】:

    从数组的字典中创建一个数据框:

    In [571]: df = pd.DataFrame({'a':['one','two','three'], 'b':np.arange(3), 'c':np.ones(3)})
    In [572]: df
    Out[572]: 
           a  b    c
    0    one  0  1.0
    1    two  1  1.0
    2  three  2  1.0
    

    注意混合列数据类型:

    In [579]: df.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 3 entries, 0 to 2
    Data columns (total 3 columns):
     #   Column  Non-Null Count  Dtype  
    ---  ------  --------------  -----  
     0   a       3 non-null      object 
     1   b       3 non-null      int64  
     2   c       3 non-null      float64
    dtypes: float64(1), int64(1), object(1)
    memory usage: 200.0+ bytes
    

    如果我们从中请求一个 numpy,我们会得到一个 2d 对象 dtype 数组:

    In [580]: df.values
    Out[580]: 
    array([['one', 0, 1.0],
           ['two', 1, 1.0],
           ['three', 2, 1.0]], dtype=object)
    

    重新创建一个数据框,看起来一样,但列 dtypes 不同:

    In [581]: pd.DataFrame(df.values, columns=['a','b','c'])
    Out[581]: 
           a  b    c
    0    one  0  1.0
    1    two  1  1.0
    2  three  2  1.0
    In [582]: _.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 3 entries, 0 to 2
    Data columns (total 3 columns):
     #   Column  Non-Null Count  Dtype 
    ---  ------  --------------  ----- 
     0   a       3 non-null      object
     1   b       3 non-null      object
     2   c       3 non-null      object
    dtypes: object(3)
    memory usage: 200.0+ bytes
    

    但结构化数组确实保留了列 dtpes:

    In [587]: df.to_records(index=False)
    Out[587]: 
    rec.array([('one', 0, 1.), ('two', 1, 1.), ('three', 2, 1.)],
              dtype=[('a', 'O'), ('b', '<i8'), ('c', '<f8')])
    In [588]: pd.DataFrame(_)
    Out[588]: 
           a  b    c
    0    one  0  1.0
    1    two  1  1.0
    2  three  2  1.0
    In [589]: _.info()
    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 3 entries, 0 to 2
    Data columns (total 3 columns):
     #   Column  Non-Null Count  Dtype  
    ---  ------  --------------  -----  
     0   a       3 non-null      object 
     1   b       3 non-null      int64  
     2   c       3 non-null      float64
    dtypes: float64(1), int64(1), object(1)
    memory usage: 200.0+ bytes
    

    【讨论】:

      猜你喜欢
      • 2015-04-30
      • 1970-01-01
      • 2020-11-09
      • 2015-09-01
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多