Python Forum
From Dictionaries to Matrix.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
From Dictionaries to Matrix.
#1
Hi
I have a rather complicated task.

I would like to turn the keys of my dictionaries to columns of a matrix (or columns of a data frame).

1.Not all dictionaries have all the keys so when creating the columns of matrix and dataframes and at the same time different dictionaries have same keys.
2. The rows will refer to the dictionary. Dictionaries are named with a number at the end _1,_2,_3. So each row should have dictionary values filled under the appropriate column.

My idea for now is to build two for loops.
A.One for loop for going through each dictionary separately. From dictionary_1 to dictionary_10. Get keys. Append key to the list of column names, only if it does not exist already.
B. Then a second loop for each elements of each dictionary. Find which is the appropriate column for each key and write the value.

Is there though a more straightforward way to do that in python. Two nested for loops, does not look very optimal and I am sure that python is quite advanced for handling such tasks.

Thanks
Alex
Reply
#2
so long as you don't nest the loops, and run one after the other, this method will be quite fast.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,304 May-03-2021, 06:30 AM
Last Post: Gribouillis
  matrix from matrix python numpy array shei7141 1 3,642 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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