0

我正在尝试探索 Pandas 库,并通过一个我经常遇到的例子停下来,我认为 pandas 有解决方案。给定以下代码:

In [63]: d1 = np.random.rand(3,3)
In [63]: d2 = np.random.rand(3,3)

In [64]:s1 = pandas.Series(d1,index = [['a1']*d1.shape[0],
                             [4]*d1.shape[0],
                             range(d1.shape[0])])

Out[64]:a1  4  0    [ 0.00881133  0.71344668  0.03611378]
               1    [ 0.37328776  0.63195947  0.23000941]
               2    [ 0.68466443  0.85891677  0.31740809]

In [65]: s2 = pandas.Series(d2,index = [['a2']*d2.shape[0],
                             [5]*d2.shape[0],
                             range(d2.shape[0])])
Out[65]:a2  5  0    [ 0.00881133  0.71344668  0.03611378]
               1    [ 0.37328776  0.63195947  0.23000941]
               2    [ 0.68466443  0.85891677  0.31740809]

s = s1.append(s2)

a1  4  0    [ 0.00881133  0.71344668  0.03611378]
       1    [ 0.37328776  0.63195947  0.23000941]
       2    [ 0.68466443  0.85891677  0.31740809]
    5  0    [ 0.00881133  0.71344668  0.03611378]
       1    [ 0.37328776  0.63195947  0.23000941]
       2    [ 0.68466443  0.85891677  0.31740809]

如何在没有标签的情况下单独获取所有数据矩阵的列表?

4

2 回答 2

2

s.values will do the trick.

From the documentation: DataFrame.values Convert the frame to its Numpy-array matrix representation."

I think you mean pandas.DataFrame above (not Series). Series.values exists as well.

于 2012-08-31T19:07:29.207 回答
1

我在运行您的代码时遇到错误。但是,要将 pandas Series 转换为 numpy 数组,请使用该pandas.Series.values 方法。Wes 的文档做得非常好。花点时间复习...

于 2012-08-31T17:26:00.290 回答