Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

django - Is there a way to change id of data frames appended in Pandas?

so I have two data frames, and used .append() method to merge them vertically. However, I get an error of which I assume the cause is duplicate id's. Here is a sample of the data frames.

   A   B
1  a1 b1
2  a2 b2
3  a3 b3
4  a4 b4
   A  B
1  q1 w1
2  q2 w2
3  q3 w3
4  q4 w4

Now the merged data frame looks like this:

   A  B
1 a1 b1
2 a2 b2
3 a3 b3
4 a4 b4
1 q1 w1
2 q2 w2
3 q3 w3
4 q4 w4

Now, I'd like to change the id of each row. Would that be possible? Thanks.

question from:https://stackoverflow.com/questions/65880933/is-there-a-way-to-change-id-of-data-frames-appended-in-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use ignore_index=True parameter in DataFrame.append:

df1.append(df2, ignore_index=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...