Skip to content Skip to sidebar Skip to footer

Concatenating Dictionaries Of Numpy Arrays Of Different Lengths (avoiding Manual Loops If Possible)

I have a question similar to the one discussed here Concatenating dictionaries of numpy arrays (avoiding manual loops if possible) I am looking for a way to concatenate the values

Solution 1:

One way is to go is use a dictionary of Series (i.e. the values are Series rather than arrays):

In [11]: d2
Out[11]: {'r': array([ 0.3536318 ,  0.29363604,  0.91307454]), 's': array([46])}

In [12]: d2 = {name: pd.Series(arr) for name, arr in d2.iteritems()}

In [13]: d2
Out[13]:
{'r': 0    0.353632
1    0.293636
2    0.913075
dtype: float64,
 's': 0    46
dtype: int64}

That way you can pass it into the DataFrame constructor:

In [14]: pd.DataFrame(d2)
Out[14]:
          r   s
0  0.353632  46
1  0.293636 NaN
2  0.913075 NaN

Post a Comment for "Concatenating Dictionaries Of Numpy Arrays Of Different Lengths (avoiding Manual Loops If Possible)"