0

I'm a neophyte to pandas and have been struggling to convert a Dict to a df using pd.DataFrame(Dict). Here's further detail: This Dict is part of a for loop that in every iteration reads in a new input file. As a result, Dict Values (lists) update every time and take different List sizes. The problem is, my code fails to execute pd.DataFrame(Dict) once Dict contains blank lists (Values) for all Keys and spits out: "ValueError: If using all scalar values, you must pass an index"

Dict = {'Title': [],
'Organization': [],
'City': [],
'Company': []}

Could anybody shed some light on this? Thanks a million in advance.

Riccardo
  • 115
  • 11

1 Answers1

0

Running your dictionary example through the pd.DataFrame(Dict) command does not give me any errors, I just get an empty DataFrame. And that's how it should be, as those empty lists you have as values are not scalars, they are iterables. This ValueError shows up when all values are in fact scalars, e.g., integers/floats/strings. If you just convert those scalars into iterables, such as empty lists or lists each containing one element (e.g., ['New York']), you should not get any errors.

Per this SO thread, there are many suggested ways to handle this error when you actually have scalars, such as actually passing an index to pd.DataFrame() as a list, as requested by the error message. You just need to make the size of that index list the same as the number of elements you are feeding to the DataFrame.

AlexK
  • 2,855
  • 9
  • 16
  • 27