2

I'm trying to implement a functionality converting longer columns to widers columns in pydatatable, in this process i have come across an issue with dictionary which has different sizes each key as demonstrated in the below code.

long_to_wide_dict = {
    'eggs':[3,5,6,9],
    'fruits':[1,2,3,4,5],
    'chicken':[5,10,2],
    'beef':[10,10],
    'bread':[5,4,3,2,1] 
}

I'm passing this dict to a Frame object as

dt.Frame(long_to_wide_dict)

Here its throwing out an error as

ValueError: Column 1 has different number of rows (5) than the preceding columns (4)

It's obvious that each key should have equal size of values when a data structure passed to Frame. So, is there any option avail like Force to fill in NA's to key which is having lesser values so that each key would come to equal sizes.

Would you have any other suggestions for it ?.

Pasha
  • 6,298
  • 2
  • 22
  • 34
myamulla_ciencia
  • 1,282
  • 1
  • 8
  • 30

1 Answers1

3

There is no force parameter in Frame constructor, but there is one in dt.cbind(). So you can first create separate frames for each key/value in your dictionary, and then cbind them into a single frame using force=True:

>>> dt.cbind([dt.Frame({k:v}) for k,v in long_to_wide_dict.items()], force=True)
   | eggs  fruits  chicken  beef  bread
-- + ----  ------  -------  ----  -----
 0 |    3       1        5    10      5
 1 |    5       2       10    10      4
 2 |    6       3        2    NA      3
 3 |    9       4       NA    NA      2
 4 |   NA       5       NA    NA      1

[5 rows x 5 columns]
Pasha
  • 6,298
  • 2
  • 22
  • 34
  • 2
    wow, got it. i would try it. thanks for the quick help and with these concepts i can go ahead and implement functions to reshape DATATABLE columns(wide to long,long to wide) – myamulla_ciencia May 21 '20 at 17:23