Home > Net >  Merge DataFrames in Python
Merge DataFrames in Python

Time:10-02

I am trying to merge 2 dataframes with some indexes repeated, which i want to be replaced.

This is what i want to do with a simple example. I have this Data Frames:

df1 = pd.DataFrame({'values': [1, 2, 3]}, index=['a', 'b', 'c'])
df2 = pd.DataFrame({'values': [4, 5, 6]}, index=['c', 'd', 'e'])

and i want to merge them in a way the result is a DataFrame with index= ['a', 'b', 'c', 'd', 'e'] and values = [1, 2, 4, 5, 6]. Since index 'c' is in both DataFrames i want to replace the value with the new one and in the cases where there is no matching index, the index value are added as a new row.

How can i do this?

CodePudding user response:

import pandas as pd
from pandas import Series, DataFrame

df1 = pd.DataFrame({'values': [1, 2, 3]}, index=['a', 'b', 'c'])
df2 = pd.DataFrame({'values': [4, 5, 6]}, index=['c', 'd', 'e'])

df_new = pd.concat([df1, df2], axis=0)
print(df_new)

df_new1 = df_new.groupby(df_new.index).first()
print(df_new1)

df_new2 = df_new.groupby(df_new.index).last()
print(df_new2)
   values
a       1
b       2
c       3
c       4
d       5
e       6
   values
a       1
b       2
c       3
d       5
e       6
   values
a       1
b       2
c       4
d       5
e       6
  • Related