Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct pandas dataframe from series of arrays

Hi I have the following pandas Series of numpy arrays:

 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]

Do you know how I could convert it into a dataframe with 3 columns? Thanks!

like image 912
NickD1 Avatar asked Sep 15 '15 19:09

NickD1


People also ask

How do you create a DataFrame using multiple series?

You can create a DataFrame from multiple Series objects by adding each series as a columns. By using concat() method you can merge multiple series together into DataFrame. This takes several params, for our scenario we use list that takes series to combine and axis=1 to specify merge series as columns instead of rows.

How do you combine arrays into data frames?

To convert an array to a dataframe with Python you need to 1) have your NumPy array (e.g., np_array), and 2) use the pd. DataFrame() constructor like this: df = pd. DataFrame(np_array, columns=['Column1', 'Column2']) .

Can you create a pandas series from an array?

A pandas Series is very similar to a 1-dimensional NumPy array, and we can create a pandas Series by using a NumPy array.


2 Answers

Feeding a list of lists to pd.DataFrame is a more efficient approach:

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

Benchmarking on 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
like image 59
jpp Avatar answered Oct 17 '22 12:10

jpp


It won't be super-performant, but you should be able to 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
like image 7
DSM Avatar answered Oct 17 '22 12:10

DSM