【问题标题】:Left join and ValueError: Wrong number of items passed 55, placement implies 1Left join 和 ValueError:错误的项目数通过 55,位置暗示 1
【发布时间】:2019-02-25 17:52:34
【问题描述】:

我有两个数据框

  • 一大块:myTradeFrame (7401x27)
  • 一小部分:specialsData (3x3)

specialsData 如下所示:

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

然后代码是这样的:

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

即使两个数据帧中都存在关键列,这行代码也会给我一个错误。我错过了什么?

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'

基本上,myTradeFrame 中的 NewColumn 应该在 coll_cusip 和 tran_type 的交点处具有“maturity_max”列的值

【问题讨论】:

  • 能否请您在两个表中添加 1. 错误日志 2. coll_cusip 和 tran_type 的数据类型
  • 谢谢。 specialsData 的数据类型为 coll_cusip object tran_type object maturity_max float64,myTradeFrame 的数据类型为 coll_cusip object tran_type object ,错误信息在问题中更新。
  • 很糟糕,我没有看到您的代码。您只需将结果保存在不同的数据框中。 pd.merge 返回一个数据框。

标签: python pandas dataframe left-to-right


【解决方案1】:

pd.merge 返回分配给单个列的数据框。 因此错误。您可以使用以下代码替换您的代码代码

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

提示:如果左右数据框中的键相同,则使用 on。

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 2019-09-23
    • 2016-02-14
    • 2020-09-17
    • 2021-12-12
    • 2021-04-08
    • 2020-01-30
    • 2021-07-13
    • 2023-03-24
    相关资源
    最近更新 更多