简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM