I want to compare a column of a DataFrame with an array. If the value of the column and the array are equal, it shold save the value of this row and another column in a new array.
I got the problem that sometimes it can't find the row with the equal number even there is one.
import numpy as np
import pandas as pd
input1=np.arange(0.,1.,0.1)
output1=np.arange(1.,0.,-0.1)
df1= pd.DataFrame(columns=['input', 'output'])
df1['input']=input1
df1['output']=output1
in1=np.arange(0.9,0.,-0.1)
in2=np.arange(0.,0.9,0.1)
in_func=np.concatenate((in1, in2), axis=0)
b=np.zeros((len(in_func)))
for i in range(len(in_func)):
a = df1.loc[df1['input']==in_func[i], 'output']
b[i] = a.iloc[0] #just for explaining my problem
The output for a
is:
9 0.1
Name: output, dtype: float64
8 0.2
Name: output, dtype: float64
7 0.3
Name: output, dtype: float64
6 0.4
Name: output, dtype: float64
Series([], Name: output, dtype: float64)
Series([], Name: output, dtype: float64)
Series([], Name: output, dtype: float64)
Series([], Name: output, dtype: float64)
Series([], Name: output, dtype: float64)
0 1.0
Name: output, dtype: float64
1 0.9
Name: output, dtype: float64
2 0.8
Name: output, dtype: float64
3 0.7
Name: output, dtype: float64
4 0.6
Name: output, dtype: float64
5 0.5
Name: output, dtype: float64
6 0.4
Name: output, dtype: float64
7 0.3
Name: output, dtype: float64
8 0.2
Name: output, dtype: float64
I get the error "IndexError: single positional indexer is out-of-bounds", because there are some empty series for in_func=[0.5, 0.4, 0.3, 0.2, 0.1]
. I don't know why they are empty, the second time this values are in the in_func
it is working.
Can someone help me? Thank you very much for your help.