MPa
MPa

Reputation: 1142

Mapping numpy arrays into pandas DataFrame results in ValueError

Say I have a dictionary with keys: arrays, such as:

In[0]:  arrs = {
  ...:      'a': np.array([1, 2, 3]),
  ...:      'b': np.array([4, 5, 6])
        }

And a pandas DataFrame whose index contains these keys:

 In[1]:  df = pd.DataFrame(index=list('abc'), columns = list('def'))
   ...:  df
Out[1]:
        d   e   f
    a   NaN NaN NaN
    b   NaN NaN NaN
    c   NaN NaN Na

I would like to populate the DataFrame with the values from the array dictionary.

This works:

In[2]:  for idx in ['a', 'b']:
  ...:      df.loc[idx, :] = arrs[idx]
  ...:  df
Out[2]: 

            d   e   f  
       a    1   2   3  
       b    4   5   6  
       c    NaN NaN NaN  

Which is fine, but I would like to vectorize the operation. I tried what I thought would work:

In[3]:  df.loc[('a', 'b'), :] = df.loc[('a', 'b'), :].index.map(lambda x: arrs[x])

But this results in a ValueError:

ValueError: could not broadcast input array from shape (2) into shape (2,3)

Why is my mapping only counting the number of arrays, and not actually seeing the shape of the arrays?

Upvotes: 1

Views: 225

Answers (1)

ALollz
ALollz

Reputation: 59519

Use the DataFrame constructor on your dictionary, then update the first DataFrame.

import pandas as pd

df.update(pd.DataFrame.from_dict(arrs, orient='index', columns=['d', 'e', 'f']))

Output: df

     d    e    f
a    1    2    3
b    4    5    6
c  NaN  NaN  NaN

Upvotes: 2

Related Questions