I just want to add element in filtered index. Here's my code, A
is dataframe and index
is a column
my_input = A["index"] 'a.age'
ML_filtered = ML["index"].isin(my_input)
Here's my error message
UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')
What's wrong with the code?
Edit
I try my_input = A["index"] 'a.age'
The error is
TypeError: Cannot broadcast np.ndarray with operand of type <class 'list'>
CodePudding user response:
There is list/array in column A['index']
, possible solution is flatten it bySeries.explode
:
A = pd.DataFrame({'index':[[1,2],[2,5]]}).applymap(np.array)
print (A)
index
0 [1, 2]
1 [2, 5]
ML = pd.DataFrame({'index':['1a.age','5a.age', 'tmp']})
my_input = A["index"].explode().astype(str) 'a.age'
ML_filtered = ML["index"].isin(my_input)
print (ML_filtered)
0 True
1 True
2 False
Name: index, dtype: bool