Here's what I'm working with:
import numpy as np
import pandas as pd
np.__version__ # '1.8.1'
pd.__version__ # '0.14.1-107-g381a289'
Here is some fake data:
foo = np.arange(5)
bar = np.random.randn(30).reshape((5, 3, 2))
I want to get foo
and bar
into a pd.DataFrame
.
To my surprise, the following doesn't work (even though foo.shape[0] == bar.shape[0]
):
df = pd.DataFrame.from_dict(dict(foo=foo, bar=bar))
# Exception: Data must be 1-dimensional
This does work:
df = pd.DataFrame.from_dict(dict(foo=foo.tolist(), bar=bar.tolist()))
df['bar'] = df['bar'].apply(np.array)
This roundabout method of converting my array
to a nested list
and then converting it back to an array via apply
is bothersome. Is there a more straightforward way that I'm just not aware of?
df
really what you want? – DSM Aug 1 '14 at 1:20