我在代码的np.select
部分遇到了一个问题,并复制了一个最小可复制示例,以寻求一些建议,说明为什么返回ValueError: -1 is not in range
而不是nan
import numpy as np
import pandas as pd
test = {'number' : [1,2,3,4,5,6]}
df = pd.DataFrame(data=test)
print(df)
number = 1
#check row index of first value less than 'number'
print((np.abs(df['number']-number)).values.argmin()-1)
conditions = [number <= df['number'][3], number > df['number'][3]]
selection = [df['number'][(np.abs(df['number']-number)).values.argmin()-1], 'ignore'] # get first value in df['number'] column less than 'number' variable
answer = np.select(conditions, selection, default=np.nan)
print(answer)
当number = 1
时使用df['number'][3]
,我希望返回nan
,因为位于df['number'][3]
中的值是4,并且尽管number = 1
小于4,但在df['number']
中的行索引上方没有值为1
的行
相反,我得到的是ValueError: -1 is not in range
而不是nan
修改代码以避免上述错误-这只是原始代码的典型版本,但有轻微修改和if statement
:
import numpy as np
import pandas as pd
test = {'number' : [1,2,3,4,5,6]}
df = pd.DataFrame(data=test)
print(df)
number = 1
#check row index of first value less than 'number'
row_index_less =(np.abs(df['number']-number)).values.argmin()-1
print(row_index_less)
if row_index_less>-1:
conditions = [number <= df['number'][3], number > df['number'][3]]
selection = [df['number'][row_index_less], 'ignore']
# get first value in df['number'] column less than 'number' variable
answer = np.select(conditions, selection, default=np.nan)
else
answer = np.nan
print(answer)