我是python新手。在这里,我要替换数组。
def do_smoothing(input_csv,original):
print(input_csv)
for ins, rw in input_csv.iterrows():
if(rw.start or rw.end != -999999):
order_value_equal = comp(rw.Previous_Three, rw.Next_Three)
two_equal = eval_tuples(rw.Previous_Three, rw.Next_Three)
check_fist_fourth = chcek_offset0_and_offset4(rw.Previous_Three, rw.Next_Three,ins)
check_two_zeros_onEither_side_val = check_two_zeros_onEither_side(rw.Previous_Three, rw.Next_Three,ins)
#print(type(rw.start))
#print(type(rw.end))
if order_value_equal:
original = np.array(original)
original[rw.start + 1: rw.end -1] = get_maximum_value(rw.Previous_Three)
#original=np.where(original['OFFSET'].between(int(rw.start)+1,int(rw.end)-1),get_maximum_value(rw.Previous_Three),original['PredictedFeature'])
#original =
if two_equal:
original = np.array(original)
original[rw.start + 1: rw.end -1] = get_maximum_value(rw.Previous_Three)
#second_total = original['OFFSET'].between(int(rw.start), int(rw.end),inclusive= False).value_counts()
#original['PredictedFeature']=np.where(original['OFFSET'].between(int(rw.start)+1,int(rw.end)-1),get_maximum_value(rw.Previous_Three),original['PredictedFeature'])
if check_fist_fourth:
original = np.array(original)
original[rw.start + 1: rw.end -1] = get_firstvalue(rw.Previous_Three)
#original['PredictedFeature']=np.where(original['OFFSET'].between(int(rw.start)+1,int(rw.end)-1),get_firstvalue(rw.Previous_Three),original['PredictedFeature'])
if check_two_zeros_onEither_side_val:
original = np.array(original)
original[rw.start + 1: rw.end -1]= get_firstvalue(rw.Previous_Three)
#original['PredictedFeature']=np.where(original['OFFSET'].between(int(rw.start)+1,int(rw.end)-1),get_firstvalue(rw.Previous_Three),original['PredictedFeature'])
else:
pass
return original
在此情况下,我每次都在更新此数组,但返回时不返回更新的数组,而是返回未更新的数组。有人可以帮我吗?
input_csv
是从CSV文件派生的pandas DataFrame对象。假设基于iterrows
方法是该数据类型的熊猫方法。original
是默认的Python list
,正在将其转换为numpy array
。comp
,eval_tuples
等方法是产生布尔值的自定义方法。get_maximum_value
和get_firstvalue
之类的方法是自定义方法,可产生int
或其他基本数据结构。def do_smoothing_better(input_csv, original):
print(input_csv)
for ins, row in input_csv.iterrows():
if row.start or row.end != -999999:
order_value_equal = comp(row.Previous_Three, row.Next_Three)
two_equal = eval_tuples(row.Previous_Three, row.Next_Three)
check_fist_fourth = check_offset0_and_offset4(
row.Previous_Three, row.Next_Three, ins
)
check_two_zeros_onEither_side_val = check_two_zeros_onEither_side(
row.Previous_Three, row.Next_Three, ins
)
else: # this fallback must be added so you don't end up
continue # with errors if no booleans are actuall initialized
if order_value_equal or two_equal: # both of these can be combined
original = np.array(original) # as they resulted in the same expression
original[row.start + 1 : row.end - 1] = get_maximum_value(
row.Previous_Three
)
elif check_fist_fourth or check_two_zeros_onEither_side_val: # as can these
original = np.array(original)
original[row.start + 1 : row.end - 1] = get_firstvalue(row.Previous_Three)
else:
continue
return original
添加适当的else
测试并对第二个条件进行这样的格式化应该可以防止解释器放入错误的块中,从而产生不准确的值。