Posts: 2
Threads: 1
Joined: Apr 2022
I am trying to transform this kind of dic to classic dataframe.
list = [
{
"Fruit": [{"XY": 15.2,22,22 },
{"XX": 19, 23,23},
{"XXX": 17.8,24,24}
]
}
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?
Posts: 6,779
Threads: 20
Joined: Feb 2020
Apr-13-2022, 06:26 PM
(This post was last modified: Apr-13-2022, 06:26 PM by deanhystad.)
df = pd.DataFrame({"XY": [15.2,22,22], "XX": [19, 23,23], "XXX": [17.8,24,24]})
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 don't understand your data. Maybe it should be like this:
foods = { # Do not use "list" as a variable name. "list" is the name of a built-in function.
"Fruit":{"XY": [15.2,22,22], "XX": [19, 23,23], "XXX": [17.8,24,24]}
}
df = pd.DataFrame(foods)
print(df) Output: Fruit
XX [19, 23, 23]
XXX [17.8, 24, 24]
XY [15.2, 22, 22]
Posts: 2
Threads: 1
Joined: Apr 2022
(Apr-13-2022, 06:26 PM)deanhystad Wrote: df = pd.DataFrame({"XY": [15.2,22,22], "XX": [19, 23,23], "XXX": [17.8,24,24]})
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 don't understand your data. Maybe it should be like this:
foods = { # Do not use "list" as a variable name. "list" is the name of a built-in function.
"Fruit":{"XY": [15.2,22,22], "XX": [19, 23,23], "XXX": [17.8,24,24]}
}
df = pd.DataFrame(foods)
print(df) Output: Fruit
XX [19, 23, 23]
XXX [17.8, 24, 24]
XY [15.2, 22, 22]
Many thanks for reply but now i am getting this kind of error
If using all scalar values, you must pass an index
Posts: 12,022
Threads: 484
Joined: Sep 2016
recheck your code, deanhystad's solution works for me.
Did you remember to change the name of your first list?
You cannot use a reserved word.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(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.
|