如何从一系列数组构造pandas数据帧

时间:2015-09-15 19:30:58

标签: python arrays pandas numpy dataframe

您好我有以下pandas系列numpy数组:

 datetime
    03-Sep-15     [53.5688348969, 31.2542494769, 18.002043765]
    04-Sep-15     [46.845084292, 27.0833015735, 15.5997887379]
    08-Sep-15    [52.8701581666, 30.7347431703, 17.6379377917]
    09-Sep-15    [47.9535624339, 27.7063099999, 15.9126963643]
    10-Sep-15     [51.2900606534, 29.600945626, 16.8756260105]

您知道如何将其转换为包含3列的数据框吗?谢谢!

2 个答案:

答案 0 :(得分:5)

它不具备超高性能,但您应该能够apply(pd.Series)

>>> ser
03-Sep-15     [53.5688348969, 31.2542494769, 18.002043765]
04-Sep-15     [46.845084292, 27.0833015735, 15.5997887379]
08-Sep-15    [52.8701581666, 30.7347431703, 17.6379377917]
09-Sep-15    [47.9535624339, 27.7063099999, 15.9126963643]
10-Sep-15     [51.2900606534, 29.600945626, 16.8756260105]
dtype: object
>>> type(ser.values[0])
<class 'numpy.ndarray'>
>>> ser.apply(pd.Series)
                   0          1          2
03-Sep-15  53.568835  31.254249  18.002044
04-Sep-15  46.845084  27.083302  15.599789
08-Sep-15  52.870158  30.734743  17.637938
09-Sep-15  47.953562  27.706310  15.912696
10-Sep-15  51.290061  29.600946  16.875626

答案 1 :(得分:2)

将列表列表提供给pd.DataFrame是一种更有效的方法:

s = pd.Series([np.array([53.5688348969, 31.2542494769, 18.002043765]),
               np.array([46.845084292, 27.0833015735, 15.5997887379]),
               np.array([52.8701581666, 30.7347431703, 17.6379377917]),
               np.array([47.9535624339, 27.7063099999, 15.9126963643]),
               np.array([51.2900606534, 29.600945626, 16.8756260105])],
              index=['03-Sep-15', '04-Sep-15', '08-Sep-15', '09-Sep-15', '10-Sep-15'])

df = pd.DataFrame(s.values.tolist(), index=s.index)

print(df)

                   0          1          2
03-Sep-15  53.568835  31.254249  18.002044
04-Sep-15  46.845084  27.083302  15.599789
08-Sep-15  52.870158  30.734743  17.637938
09-Sep-15  47.953562  27.706310  15.912696
10-Sep-15  51.290061  29.600946  16.875626

在Python 3.6 / Pandas 0.19上进行基准测试:

%timeit pd.DataFrame(s.values.tolist(), index=s.index)  # 448 µs per loop
%timeit s.apply(pd.Series)                              # 1.5 ms per loop