我将反向数据帧附加到其原始数据帧。似乎一切正常,但我意识到这一点:df1起初有ids(1,1,1,2,2) 。我将函数df2['id'] = df2['id'].apply(lambda x: x + id_amount)应用于df2not todf1但无论如何,当我附加df2到df1它的ids更改时。怎么可能?为什么df1取df2id列的值import pandas as pddf1 = pd.DataFrame({'x':[1,1,2,9,9], 'y':[1,2,2,100,101],'id':[1,1,1,2,2]})df2= df1[::-1] #df2 as reverse of df1print(df1)id_amount=df2['id'].nunique()df2['id'] = df2['id'].apply(lambda x: x + id_amount) df2=df2.sort_values(by=['id'])df1=df1.append(df2)df1 = df1.reset_index(drop=True)print(df1)这里df1之前和之后:#beforex y id 1 1 1 1 2 1 2 2 1 9 100 2 9 101 2 变成:#afterx y id 1 1 3 1 2 3 2 2 3 9 100 4 9 101 4 2 2 3 1 2 3 1 1 3 9 101 4 9 100 4应该变成:x y id 1 1 1 1 2 1 2 2 1 9 100 2 9 101 2 2 2 3 1 2 3 1 1 3 9 101 4 9 100 4
添加回答
举报
0/150
提交
取消