"函数返回的数据框覆盖之前的变量赋值"

2024-05-16 23:36:36 发布

您现在位置:Python中文网/ 问答频道 /正文

从一个函数中,我调用另一个函数,返回一个dataframe,每次返回一个新的dataframe,并将它赋给一个变量test\u 1。在同一个函数中,我再次调用另一个函数,并将返回的数据帧分配给另一个测试2。当我打印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

在第4行,test\u 1返回的数据帧与第6行test\u 2返回的数据帧不同,但在第7行,test\u 1和test\u 2中的数据帧相同。你知道吗

# 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是一个数据帧。你知道吗


Tags: 数据函数nametestdataframenewreturndef