I have a pandas dataframe. I want to fill some of the cells with numpy array but I get the following ValueError.
I wil not fill with zero array in real life. This is the simplified example code to replicate the error
ValueError: could not broadcast input array from shape (10,) into shape (1,)
JavaScript
x
11
11
1
import pandas as pd
2
import numpy as np
3
4
df = pd.DataFrame(columns=['name1','name2','array1','array2' ])
5
df = df.append({'name1': 'aaaa','name2': 'bbbb','array1':np.nan,'array2': np.nan}, ignore_index=True)
6
df = df.append({'name1': 'cccc','name2': 'dddd','array1':np.nan,'array2': np.nan}, ignore_index=True)
7
8
df.loc[((df['name1']=='aaaa') & (df['name2']=='bbbb')),'array1']=np.zeros((10,1))
9
10
print(df)
11
Advertisement
Answer
One workaround solution is to use .map()
with filtering of cell with .loc
as you did, as follows:
This works since .map()
works on transforming element-wise and would not try to broadcast array to the whole series.
JavaScript
1
16
16
1
df.loc[((df['name1']=='aaaa') & (df['name2']=='bbbb')),'array1'] = df.loc[((df['name1']=='aaaa') & (df['name2']=='bbbb')),'array1'].map(lambda x: np.zeros((10,1)))
2
3
4
print(df)
5
6
name1 name2 array1 array2
7
0 aaaa bbbb [[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]] NaN
8
1 cccc dddd NaN NaN
9
10
11
df.applymap(type) # to check the data type
12
13
name1 name2 array1 array2
14
0 <class 'str'> <class 'str'> <class 'numpy.ndarray'> <class 'float'>
15
1 <class 'str'> <class 'str'> <class 'float'> <class 'float'>
16