-3

I am writing code that turns a dataframe into a list of dictionaries. Using to_dict('list'), it returns a dictionary that uses the column headers as the key and the rows in the column as values in a list. Is there a way to specify how many lists I want? I want it to return lists of 3 elements each.

This isn't the same as splitting a list into chunks as its a list of values in a dictionary.

#Original:

{Col_1 : [0, 1, 2, 3, 4, 5]}

#New:

split = 3
{Col_1: [0, 1, 2], [3, 4, 5]}
  • 1
    Welcome to Stack Overflow. When trying to solve a problem, try to focus on the actual cause of the problem. It does not matter that this value is a dict key - it's a list, and the part you apparently find difficult is splitting the list up into chunks. If finding a way to do that doesn't solve the problem for you, then you actually have more than one question. If it does, then you can focus the question further - which helps you both with [looking for existing solutions](https://meta.stackoverflow.com/questions/261592) and with creating a [mre]. – Karl Knechtel May 16 '22 at 01:41
  • 1
    `{Col_1: [0, 1, 2], [3, 4, 5]}` is not a valid dictionary representation. You either do: `{Col_1: [[0, 1, 2], [3, 4, 5]]}` or `{Col_1: [0, 1, 2], Col_1: [3, 4, 5]}`. Note, the former is exactly what @Karl alluded to. Moreover, this question is confusing because you show dictionaries but also mention Pandas yet there's no sign of Pandas in your code so the end result is ambiguous. – Gene Burinsky May 17 '22 at 18:26

0 Answers0