代码之家  ›  专栏  ›  技术社区  ›  Jeremy L.

返回数据帧的函数覆盖以前的变量赋值

  •  0
  • Jeremy L.  · 技术社区  · 6 年前

    从函数中,我调用另一个函数,该函数返回一个数据帧,每次返回一个新的数据帧,并将其分配给一个变量test_1。在同一个函数中,我再次调用另一个函数,并将返回的数据帧分配给另一个测试。当我同时打印test_1和test_2时,它们都是相同的数据帧。

    这是我的代码:

    def population(count, curve_set_origin):
        test = []
        test_1 = individual(1, curve_set_origin)
        print(test_1)
        test_2 = individual(2, curve_set_origin)
        print(test_2)
        print([test_1, test_2])
        return test
    

    # Individual
    def individual(name, curve_set_origin):
        new_curve = pd.DataFrame
        curve_set_origin.iloc[:,9:] = curve_set_origin.iloc[:,9:].apply(lambda x: x.apply(lambda y: y + get_random() if y != 0 else 0), axis=1)
        new_curve = curve_set_origin
        new_curve['name'] = name
        return new_curve
    
    # Define random number between XX and XX with 4 decimal
    def get_random():
        while True:
            val = float(random.randrange(-9999, 9999)/10**(random.randint(5,9)))
            if val != 0:
                return val
    

    curve_set_origin

    0 回复  |  直到 6 年前