0

I'm joining two dataframes using merge in pandas, but the result contains columns from single dataframe. Here is the piece of code:

df1
date(index) | Total | Unique

df2
date(index) | Total | Unique

result = pd.merge(df1, df2, how="inner")
Output:
Total | Unqiue

Expected Output:
df1_Total | df2_Unique | df2_total |  df2_unique

I've also tried suffixes, but they doesn't work as well.

cs95
  • 379,657
  • 97
  • 704
  • 746
Sandie
  • 869
  • 2
  • 12
  • 22
  • What is your join key? `pd.merge` is joining `on` the following criteria if not stated: If on is None and not merging on indexes then this defaults to the intersection of the columns in both DataFrames. – ycx Mar 07 '19 at 07:28

3 Answers3

2
result = pd.merge(df1, df2, how="inner", on='date(index)')

This will join on date(index). Change your join key as necessary to what you require

ycx
  • 3,155
  • 3
  • 14
  • 26
1

It is possible by DataFrame.add_prefix and left_on and right_on parameetrs, but get same values in both columns, because default inner join:

df1 = pd.DataFrame([(10,2),(20,4),(30,6)], 
                   columns=['Total','Unique'], 
                   index=pd.date_range('2015-01-01', periods=3))
df2 = pd.DataFrame([(10,2),(30,6),(20,600)], 
                    columns=['Total','Unique'], 
                    index=pd.date_range('2015-01-02', periods=3))

#print (df1)
#print (df2)

result = pd.merge(df1.add_prefix('df1_'), 
                  df2.add_prefix('df2_'),
                  left_on=['df1_Total','df1_Unique'],
                  right_on=['df2_Total','df2_Unique'])
print (result)
   df1_Total  df1_Unique  df2_Total  df2_Unique
0         10           2         10           2
1         30           6         30           6

If want join by index values is necessary use left_index and right_index with suffixes:

result = pd.merge(df1, 
                  df2,
                  left_index=True,
                  right_index=True,
                  suffixes=('_df1','_df2'))
print (result)
            Total_df1  Unique_df1  Total_df2  Unique_df2
2015-01-02         20           4         10           2
2015-01-03         30           6         30           6
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Specify merge function parameter on as 'Date' column

pd.merge(df1, df2, how="inner", on="Date")
windstorm
  • 375
  • 2
  • 10