【问题标题】:join two tuples with different index and different shape连接两个具有不同索引和不同形状的元组
【发布时间】:2019-04-16 10:38:04
【问题描述】:

我有两个元组,我喜欢将它们合二为一,但是当我尝试连接、合并或加入它们时,我总是得到很多 NaN。

我的元组 row 看起来像这样:

我的元组row_ges 看起来像这样(形状:5 行,6 列):

我想将它放在一个像这种形状的数据框中(5 行,301 列):

我试过了

result=row_ges+row
result_1=row_ges.append(row,ignore_index=True)
result_2 = pd.concat([row_ges,row], axis=1, ignore_index=True)

但我总是得到类似(10 行,301 列)的形状,对于 result_2,我得到一个我不明白的错误代码:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-209-cdc656a8e828> in <module>()
      4 result=row_ges+row
      5 result_1=row_ges.append(row,ignore_index=True)
----> 6 result_2 = pd.concat([row_ges,row], axis=1, ignore_index=True)
      7 
      8 #gesamt = pd.DataFrame()

/usr/local/lib/python3.6/site-packages/pandas/core/reshape/concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    224                        verify_integrity=verify_integrity,
    225                        copy=copy, sort=sort)
--> 226     return op.get_result()
    227 
    228 

/usr/local/lib/python3.6/site-packages/pandas/core/reshape/concat.py in get_result(self)
    421             new_data = concatenate_block_managers(
    422                 mgrs_indexers, self.new_axes, concat_axis=self.axis,
--> 423                 copy=self.copy)
    424             if not self.copy:
    425                 new_data._consolidate_inplace()

/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
   5423         blocks.append(b)
   5424 
-> 5425     return BlockManager(blocks, axes)
   5426 
   5427 

/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in __init__(self, blocks, axes, do_integrity_check)
   3280 
   3281         if do_integrity_check:
-> 3282             self._verify_integrity()
   3283 
   3284         self._consolidate_check()

/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in _verify_integrity(self)
   3491         for block in self.blocks:
   3492             if block._verify_integrity and block.shape[1:] != mgr_shape[1:]:
-> 3493                 construction_error(tot_items, block.shape[1:], self.axes)
   3494         if len(self.items) != tot_items:
   3495             raise AssertionError('Number of manager items must equal union of '

/usr/local/lib/python3.6/site-packages/pandas/core/internals.py in construction_error(tot_items, block_shape, axes, e)
   4841         raise ValueError("Empty data passed with indices specified.")
   4842     raise ValueError("Shape of passed values is {0}, indices imply {1}".format(
-> 4843         passed, implied))
   4844 
   4845 

ValueError: Shape of passed values is (301, 29), indices imply (301, 9)

【问题讨论】:

  • 嗨,当您说“元组”时,我认为您的意思是“数据帧”。两个数据框之间是否有可用于连接、合并或联合它们的公共列?例如,请参阅pandas.pydata.org/pandas-docs/stable/…
  • 不,@Evan 没有公共列,我之前已经尝试过此链接,但找不到解决方案
  • 如果你运行row.shape或row_ges.shape,返回什么?
  • row.shape 是(5,295) 和row_ges.shape 返回(5,6) 如上所述@Evan
  • 嗯。我无法复制您的错误。您是否尝试过这里的解决方案? stackoverflow.com/questions/27719407/…您也可以尝试在一个或两个数据帧上重置索引。

标签: python pandas join merge concat


【解决方案1】:
row.reset_index(drop=True, inplace=True)
row_ges.reset_index(drop=True, inplace=True)
result = pd.concat([row_ges,row], axis=1)

问题在于不同的索引。使用此代码,它可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 2019-10-02
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多