Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transform Dic to dataframe
#1
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?
Reply
#2
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]
Reply
#3
(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
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 641 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  Transform 3 Columns into Single Column DaveG 8 1,923 Apr-04-2022, 08:42 AM
Last Post: Pedroski55
  How to transform from wide to long format in python shantanu97 1 1,673 Nov-21-2021, 11:53 AM
Last Post: buran
  Transform list or set regardless of nesting structure blubb 2 1,984 Mar-10-2020, 07:17 PM
Last Post: ibreeden
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,396 Jul-02-2019, 02:05 PM
Last Post: ichabod801
  transform list to tuple with takaa 2 2,880 Nov-28-2017, 10:44 PM
Last Post: takaa
  Laplace Inverse Transform Implementation jafar1363 11 15,060 Nov-17-2016, 08:52 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020