在if条件下替换数组后不返回新数组

问题描述 投票:0回答:1

我是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

在此情况下,我每次都在更新此数组,但返回时不返回更新的数组,而是返回未更新的数组。有人可以帮我吗?

python python-3.7
1个回答
0
投票
您的问题和代码尚不清楚,因此,如果不正确,请更正以下假设。

    input_csv是从CSV文件派生的pandas DataFrame对象。假设基于iterrows方法是该数据类型的熊猫方法。
  • [original是默认的Python list,正在将其转换为numpy array
  • compeval_tuples等方法是产生布尔值的自定义方法。
  • get_maximum_valueget_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测试并对第二个条件进行这样的格式化应该可以防止解释器放入错误的块中,从而产生不准确的值。
  • © www.soinside.com 2019 - 2024. All rights reserved.