LaVOZs

The World’s Largest Online Community for Developers

'; python - Merge Function in pandas library - LavOzs.Com

I'm trying to merge two data frames based on a single column. This column contains values starting with '+'. Merge function works fine for all other rows except for rows that have a plus sign. Example:

      df1                             df2                    df3(expected Output)
Key   Cost  Conversion         Key   avg cost  Revenue        Key   Cost  Revenue  
AAA   12.23     2              +BBB  22.33     1000           AAA   12.23   1000
+BBB  26.24     1              AAA   92.63     5689           +BBB  26.24   5689    
CCC   15.48     0              DDD   35.68     4567           CCC   15.48   4567 

df3(obtained Output)
Key   Cost  Revenue  
AAA   12.23   1000
+BBB  26.24       
CCC   15.48   4567 

res = df1.merge(df2[['Key', 'Revenue']], on='Key',left_index=True, how = 'left'

I can't seem to reproduce your result

df1.merge(df2, on='Key')

    Key   Cost  Conversion  avg_cost  Revenue
0   AAA  12.23           2     92.63     5689
1  +BBB  26.24           1     22.33     1000

df2 = pd.read_clipboard()

    Key  avg_cost  Revenue
0  +BBB     22.33     1000
1   AAA     92.63     5689
2   CCC     35.68     4567

df1.merge(df2, on='Key')

    Key   Cost  Conversion  avg_cost  Revenue
0   AAA  12.23           2     92.63     5689
1  +BBB  26.24           1     22.33     1000
2   CCC  15.48           0     35.68     4567
Related
How do I merge two dictionaries in a single expression?
Using global variables in a function
How to make a chain of function decorators?
How to merge a specific commit in Git
How do you merge two Git repositories?
How to merge two arrays in JavaScript and de-duplicate items
Renaming columns in pandas
How to iterate over rows in a DataFrame in Pandas?
Left join only selected columns in R with the merge() function
Is there a way to merge pandas dataframes on row and column index?