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!
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.
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']) .
A pandas Series is very similar to a 1-dimensional NumPy array, and we can create a pandas Series by using a NumPy array.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With