right now i have a DataFrame which contains in every row an entry with a Tuple containing Datas (20 Values per Tuple).
The tuple was created when trying to put an entire array into the appropriate row as an entry with the following line of code:
DataFrame=pd.DataFrame('windows20_energy_x': array_passed},dtype=float)
i already tried to add brackets like:
DataFrame=pd.DataFrame('windows20_energy_x': [array_passed]},dtype=float)
but get the error: AssertionError: Number of Block dimensions (4) must equal number of axes (2)
The array_passed:
[array([0.403, 0.153, 0.052, 0.299, 0.141, 0.056, 0.041, 0.019, 0.009,
0.067, 0.05 , 0.022, 0.014, 0.054, 0.007, 0.007, 0.002, 0.009,
0.003, 0.005]), array([0.277, 0.054, 0.101, 0.378, 0.066, 0.056, 0.047, 0.02 , 0.007,
0.057, 0.036, 0.022, 0.024, 0.045, 0.006, 0.008, 0.007, 0.007,
0.003, 0.002]), array([ 0.859, 1.905, 0.808, 9.338, 9.02 , 15.852, 2.491, 5.449,
4.896, 5.937, 3.969, 3.397, 2.666, 0.429, 0.348, 0.458,
0.527, 0.55 , 0.234, 0.174]), array([0.489, 0.105, 0.089, 0.464, 0.145, 0.043,0.035, 0.008, 0.012,
0.03 , 0.046, 0.019, 0.017, 0.194, 0.005, 0.014, 0.013, 0.009,
0.007, 0.005])]
- Is it possible to convert the tuple into arrays (So that the frame eventually contains arrays and not tuples)?
or
- Is there maybe another approach to get the DataFrame directly in the right way with arrays containing (By appropriate improvement/adjustment of the code line)?
Thanks in advance for your Answers!
CodePudding user response:
Pass dtype=object
when creating the dataframe:
DataFrame = pd.DataFrame({"windows20_energy_x": array_passed}, dtype=object)
print(type(DataFrame.loc[0, "windows20_energy_x"]))
Prints:
<class 'numpy.ndarray'>
CodePudding user response:
If I make a frame from a list of 3 arrays:
In [398]: df = pd.DataFrame({'foo':[np.arange(3),np.ones(3),np.zeros(3)]})
In [399]: df
Out[399]:
foo
0 [0, 1, 2]
1 [1.0, 1.0, 1.0]
2 [0.0, 0.0, 0.0]
In [400]: df.to_numpy()
Out[400]:
array([[array([0, 1, 2])],
[array([1., 1., 1.])],
[array([0., 0., 0.])]], dtype=object)
If I do the same, but specify dtype=float
, as you do:
In [401]: df = pd.DataFrame({'foo':[np.arange(3),np.ones(3),np.zeros(3)]}, dtype=float)
In [402]: df
Out[402]:
foo
0 (0, 1, 2)
1 (1.0, 1.0, 1.0)
2 (0.0, 0.0, 0.0)
In [403]: df.to_numpy()
Out[403]:
array([[(0, 1, 2)],
[(1.0, 1.0, 1.0)],
[(0.0, 0.0, 0.0)]], dtype=object)
It's still not not float
dtype, but apparently it has tried some sort of conversion. Filling dataframe columns with objects - arrays, lists, tuples or whatever, deviates in to one degree or other from the basic dataframe 2d character.