python - Creating pandas.DataFrame from numpy.array -
What I'm doing here:
NP import as Panda PDS As Np .___ version__ # '1.8.1' pd.___ version_ # '0.14.1-107-G381a289'
Here are some fake data:
foo = Np.arange (5) times = np.random.randn (30). Renewal ((5, 3, 2))
I want to get the foo
and bar
in a pd. In DataFrame
. To my surprise, the following does not work (even if foo.shape [0] == times [0]
):
df = pd DataFrame #from_dict (dict (foo = foo, bar = bar) # Exception: The data must be 1-dimension
This works:
df = Pd.DataFrame.from_dict (dict (foo = foo.tolist), bar = bar.tolist ()) DF ['bar'] = df ['bar']. Apply this roundabout method to change my array
in my nested list
and then apply it to > Convert back to an array via Apply
is annoying. Is there any more simple way I just do not know?
Comments
Post a Comment