Reputation: 275
I am appending a reverse dataframe to its original dataframe. Seemed like it is all working but than I realized this: df1
had the ids
(1,1,1,2,2) at first. I applied the function df2['id'] = df2['id'].apply(lambda x: x + id_amount)
to df2
not to df1
but anyways when i append df2
to df1
its ids
changed too. How can that be? Why does df1
take the df2
values of the id column
import pandas as pd
df1 = 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 df1
print(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)
Here df1 before and after:
#before
x y id
1 1 1
1 2 1
2 2 1
9 100 2
9 101 2
becomes:
#after
x 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
should become:
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
Upvotes: 0
Views: 34
Reputation: 59579
You could do this with a concat
and sort_index
, then resort at the end.
import pandas as pd
df1 = pd.DataFrame({'x':[1,1,2,9,9], 'y':[1,2,2,100,101],'id':[1,1,1,2,2]})
df1 = (pd.concat([df1, df1.assign(id=df1.id+df1.id.max()).sort_index(ascending=False)], ignore_index=True)
.sort_values('id')
.reset_index(drop=True))
x y id
0 1 1 1
1 1 2 1
2 2 2 1
3 9 100 2
4 9 101 2
5 2 2 3
6 1 2 3
7 1 1 3
8 9 101 4
9 9 100 4
Upvotes: 1