Python merging data frames and renaming column values

Viewed 39

In python, I have a df that looks like this

Name    ID
Anna    1
Sarah   2
Max     3

And a df that looks like this

Name    ID
Dan     1
Hallie  2
Cam     3

How can I merge the df’s so that the ID column looks like this

Name    ID
Anna    1
Sarah   2
Max     3
Dan     4
Hallie  5
Cam     6

This is just a minimal reproducible example. My actual data set has 1000’s of values. I’m basically merging data frames and want the ID’s in numerical order (continuation of previous data frame) instead of repeating from one each time.

3 Answers

Concatenate the two DataFrames, reset_index and use the new index to assign "ID"s

df_new = pd.concat((df1, df2)).reset_index(drop=True)
df_new['ID'] = df_new.index + 1

Output:

     Name  ID
0    Anna   1
1   Sarah   2
2     Max   3
3     Dan   4
4  Hallie   5
5     Cam   6

You can concat dataframes with ignore_index=True and then set ID column:

df = pd.concat([df1, df2], ignore_index=True)
df['ID'] = df.index + 1

Use pd.concat:

out = pd.concat([df1, df2.assign(ID=df2['ID'] + df1['ID'].max())], ignore_index=True)
print(out)

# Output
     Name  ID
0    Anna   1
1   Sarah   2
2     Max   3
3     Dan   4
4  Hallie   5
5     Cam   6
Related