import pandas as pd
import numpy as np
df=pd.DataFrame.from_dict({'Color':['black','black','white','white']},orient='index').transpose()
df['serch_index']=np.nan
对于第一个值'white'最接近的相反第二个值'black',对于2值'white'最接近的相反第二个值'black',您需要为单元格返回第二个值'black'的索引第一个值 'white' 和第二个值 'white' '。
动作算法如下:1)循环遍历列2)扫描当前值及其索引3)从索引中减去当前值与所有相反值索引的值4)找到最小值与 3) 的区别。5) 将结果写入当前值的地址
从查找最接近顶部的索引的任务来看,我认为您可以使用 iloc[:value] 参数
代码和伪代码:
df['serch_index']=[
for x in df.loc[df.iloc[:item.index,0],'Color']
if bool(re.match(r'black', x.values))==True #3) действие
min([item.index-x.index]) #4) действие
if bool(re.match(r'white', item.values))==True #2)действие
else
for y in df.loc[df.iloc[:item.index,0],'Color']
if bool(re.match(r'white', y.values))==True #3)действие
min([item.index-y.index]) #4) действие
for item in df['Color'] #1)действие
] #5 действие
print(df['serch_index'])
如何正确组织伪代码算法;例如:item.values、item.index、x.index、min()