Apr-14-2022, 09:35 AM
(Apr-13-2022, 06:02 PM)d9d9d Wrote: To output like this
XY XX XXX
0 15.2 19 17.8
1 22 23 24
2 22 23 24
How is it possible?
This is not possible. Columns in pandas are series and must have same datatype. So you can't have column which has floats and integers in it. Adding a float value to an integer series will turn the whole series values to float (adding a string to a numeric series will force the series to object datatype, but the main benefit of Pandas, i.e. vectorized computations, is lost as soon as you start using object series).
As @deanhystad also pointed out, source data provided is not valid Python. So one needs to make assumptions what the real data is. My interpretation/assumption of the data and the desired output:
import pandas as pd record = [{"Fruit": [{"XY": [15.2,22,22]}, {"XX": [19, 23,23]}, {"XXX": [17.8,24,24]}]}] table = {key: value for item in record[0]["Fruit"] for key, value in item.items()} df = pd.DataFrame(table) print(df)
Output: XY XX XXX
0 15.2 19 17.8
1 22.0 23 24.0
2 22.0 23 24.0
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.