0

Why can't I just convert a dict into a data frame?

dict = {
    "carat": 0.3,
    "cut": 64.0,
    "color": 55.0,
    "clarity": 4.25,
    "depth": 4.28,
    "table": 2.73,
    "x": 2.0,
    "y": 1.0,
    "z": 6.0
}

novo = pd.DataFrame(dict.items(), columns = dict.keys())

novo

This returns the error:

ValueError: 9 columns passed, passed data had 2 columns
unstuck
  • 563
  • 2
  • 12
  • 29

1 Answers1

0

dict.items() returns 2-tuples (key, value); a sensible columns for that would then be, well, e.g. ('key', 'value'). But that wouldn't give you the dataframe you're likely after, anyway.

If passing in a dict, Pandas expects you to pass in one with lists for values (e.g. one value per row). You can do that with a dictionary comprehension that takes each single value and wraps it in an 1-value list:

novo = pd.DataFrame({col: [value] for (col, value) in d.items()})
print(novo)

so

   carat   cut  color  clarity  depth  table    x    y    z
0    0.3  64.0   55.0     4.25   4.28   2.73  2.0  1.0  6.0
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Incredibly COMPLICATED! but thank you anyway. I've found out that if the numbers are within square brackets, then pd.DataFrame(dict) is enough. I have no idea why, and I'm giving up this shti – unstuck Dec 15 '22 at 08:25
  • 1
    It's not complicated at all once you understand the concepts. We talked about the same thing in the comments in https://stackoverflow.com/q/68792716/51685 – it's not very useful to say "numbers within square brackets"; look at https://docs.python.org/3/tutorial/introduction.html#lists instead... – AKX Dec 15 '22 at 09:46
  • I have read the documentation like a million times, and maybe some 20 Python books, and 20 online courses... It doesn't matter, if the logic does not make sense, I'll get back here and ask basic questions (like this one: https://stackoverflow.com/questions/74802773/how-to-hide-log-info-in-pycaret-using-google-colab), which makes everyone waste an awful amount of time once focused on the big picture. Looking at the code you wrote up there, I find it amazing that we need all that stuff to just convert a dict into a data frame. – unstuck Dec 15 '22 at 13:54
  • If your dict had lists for values, there'd be no need for "all that stuff". IOW, `pd.DataFrame({"a": [1, 2], "b": [3, 4]})` gives you a two-row dataframe, but Pandas doesn't understand what you want to do when some (or all!) of the values aren't lists. – AKX Dec 15 '22 at 13:57