1

Context

I have a Pandas DataFrame and a Use Case, where I need to store a Collection (Dictionary) in of the Columns. I do know, that a DataFrame is probably not designed for this, however, I am looking for a simple solution without Relationships between multiple DataFrames, Series, etc.

I am currently getting the following Error:

ValueError: Length of values (3) does not match length of index (5)


Code

procedures = {'A': 'Procedure A', 'B': 'Procedure B', 'C': 'Procedure C'}

# I would like to create a new Column 'Procedures' and assign the Dictionary above to it for every Row.
data['Procedures'] = procedures

Question

  • Is this possible with Pandas and how can I achieve it?
christophriepe
  • 1,157
  • 12
  • 47

2 Answers2

1

Repeat list of dict by multiple by length of DataFrame:

data['Procedures'] = [procedures] * len(data)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
-1

To save a Dictionary inside a Pandas DataFrame use pd.DataFrame.from_dict(input_dict).

If you think it's not clear, refer to this link.

Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
jaswanth
  • 29
  • 1
  • 1
  • 8