0

I am trying to simply merge one dataframe to another, but keep getting an unexpected output

My dataframes are:

grouped.head()

which ouputs:

           Lifetime_Revenue  Customer_ID  Lifetime 
    Cohort  
    ----------------------------------------------
    2014-01    149.90             1          NaN
    2014-02    299.90             1          NaN
    2014-03    1499.68            3          NaN
    2014-04    299.90             1          NaN
    2014-05    2249.70            3          NaN

the other is:

   Lifetime
   ---------
   0  10
   1  10
   2  11
   3  9
   4  12

I am trying to merge Lifetime from ltnew to grouped but when I try to merge the two:

merged = pd.merge(grouped,ltnew,on=['Lifetime'],right_index=True)

The output is just a flattened dataframe:

       Lifetime_Revenue   Customer_ID   Lifetime
Cohort  
------------------------------------------------    

Doing an outer and left join just reproduces an exact copy of the grouped dataframe, while doing a right just fills in the lifetime column but leaves all other columns null.

Anyone able to help?

cs95
  • 379,657
  • 97
  • 704
  • 746
Kbbm
  • 375
  • 2
  • 15
  • 1
    It looks like you misunderstood how a `merge` is supposed to work... it doesn't merge on the headers, it merges on the column _values_. You could just do `grouped['Lifetime'] = ltnew['Lifetime'].values` – cs95 Mar 26 '18 at 04:57
  • Ah I see, now. Thank you, this solved the problem! – Kbbm Mar 26 '18 at 05:02

0 Answers0