pythonpandasdataframeleft-to-right

Left join and ValueError: Wrong number of items passed 55, placement implies 1


I have two dataframes

specialsData looks like this:

   coll_cusip tran_type  maturity_max
0  912810SC3        BB          1.80
1  912810SD1        BB          1.76
2  9128284V9        BB          1.08

and then the code is this:

myTradeFrame['NewColumn']=pd.merge(myTradeFrame, specialsData, how='left', left_on = ['coll_cusip','tran_type'], right_on=[ 'coll_cusip', 'tran_type'])

this line of code gives me an error even though the key columns are present in both dataframes. What am I missing?

the error message i get is : 
    len(self.mgr_locs)))
ValueError: Wrong number of items passed 55, placement implies 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2850, in run_ast_nodes
    if self.run_code(code, result):
  File "/apps/qtrinst/install/python/anaconda/envs/sx_anaconda/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2927, in run_code
    self.showtraceback(running_compiled_code=True)
TypeError: showtraceback() got an unexpected keyword argument 'running_compiled_code'

Basically, the NewColumn in myTradeFrame should have the values of the column "maturity_max" at the intersection coll_cusip and tran_type


Solution

  • pd.merge returns a dataframe which is being assigned to a single column. Hence the error. You can replace your code code with the following

    myTradeFrame = pd.merge(myTradeFrame, specialsData, how='left', on=[ 'coll_cusip', 'tran_type'],suffixes=('_left','_right'))
    

    Tip : if keys in left and right dataframe are same just use on.