KJA
KJA

Reputation: 333

Retain index when combining two dataframes

The problem When I merge two dataframes, I lose the rownames. I want to avoid this. Note that some of the rows in the dataframes have different names.

I have tried different versions of "merge" from Pandas without success.

Code example:

df1 = pd.DataFrame()
series1 = pd.Series([1,2])
series1 .rename(index={0:'zero',1:'one'}, inplace=True)
df1['someValue'] = series1
df1['time'] = "day1"
df1

      someValue  time
zero          1  day1
one           2  day1

df2 = pd.DataFrame()
series2 = pd.Series([3,4,5])
series2 .rename(index={0:'zero',1:'one', 2:'two'}, inplace=True)
df2['someValue'] = series2
df2['time'] = "day2"
df2

      someValue  time
zero          3  day2
one           4  day2
two           5  day2

df1.merge(df2,  how='outer')
   someValue  time
0          1  day1
1          2  day1
2          3  day2
3          4  day2
4          5  day2

I want

   someValue  time
zero       1  day1
one        2  day1
zero       3  day2
one        4  day2
two        5  day2

Also note that I would want this to work when there are rows in the first df that is not in the second df, and also the other way around.

Upvotes: 1

Views: 414

Answers (1)

cs95
cs95

Reputation: 402353

The solution is to use concat:

pd.concat([df1, df2])

      someValue  time
zero          1  day1
one           2  day1
zero          3  day2
one           4  day2
two           5  day2

Upvotes: 3

Related Questions