【问题标题】:retrieve data from pandas.core.series.series and save them into matrix从 pandas.core.series.series 检索数据并将它们保存到矩阵中
【发布时间】:2021-07-10 23:44:24
【问题描述】:

我在运行程序时生成了一个变量,它的类型是series,内容如下所示。可以看出test_prior是一个系列,其中每个系列元素也是一个系列。如何检索这些元素系列并将它们保存到 numpy 数组中。或者,是否可以将其系列(即test_prior)保存到矩阵中?

type(test_prior)
pandas.core.series.Series

test_prior
0      [0.0032125000000000005, 0.003213987572590001, ...
1      [0.0037124999999999997, 0.00371353755063, 0.00...
2      [0.0003125, 0.0003170377214300023, 0.000321575...
3      [0.0048625, 0.004865587650670002, 0.0048686753...
4      [0.0020875, 0.0020882125347700003, 0.002088925...
                             ...                        
195    [0.0010625, 0.0010631875335500003, 0.001063875...
196    [0.0009875, 0.0009895876018700012, 0.000991675...
197    [0.0024375, 0.0024401126274900013, 0.002442725...
198    [0.0022375, 0.0022403376384700015, 0.002243175...
199    [0.0005125, 0.000514487596990001, 0.0005164751...
Length: 200, dtype: object

【问题讨论】:

  • 元素可能是列表,也可能是 numpy 数组。你看过test_prior.values吗?
  • 同意,它们看起来像列表/数组,而不是系列。检查type(test_prior[0])
  • type(test_prior) 是 numpy.ndarray

标签: python python-3.x pandas numpy scipy


【解决方案1】:

我认为您可以将Series 转换为DataFrame,然后使用explode

test_prior.to_frame().apply(lambda c: c.explode(), axis=1)

请注意,这仅在每行中的数组具有相同长度时才有效。但如果不是这种情况,那么将数据排列成二维似乎没有意义。

【讨论】:

  • 也可以直接应用pd.Series.explode,不用lambda:test_prior.to_frame().apply(pd.Series.explode, axis=1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 2020-11-09
  • 2020-08-02
  • 1970-01-01
相关资源
最近更新 更多