-1

I have two dataframes. df1 has a shape of (64, 10) while df2 has a shape of (64, 1).

I have been trying to concat the two but it shows error.

It's been bugging me for a while. Thanks!


pdff = pd.concat([df1, df2], ignore_index=True, sort=False, axis=1)
  File "C:\Python37\lib\site-packages\pandas\core\reshape\concat.py", line 287, in concat
    return op.get_result()
  File "C:\Python37\lib\site-packages\pandas\core\reshape\concat.py", line 503, in get_result
    mgrs_indexers, self.new_axes, concat_axis=self.bm_axis, copy=self.copy,
  File "C:\Python37\lib\site-packages\pandas\core\internals\concat.py", line 84, in concatenate_block_managers
    return BlockManager(blocks, axes)
  File "C:\Python37\lib\site-packages\pandas\core\internals\managers.py", line 149, in __init__
    self._verify_integrity()
  File "C:\Python37\lib\site-packages\pandas\core\internals\managers.py", line 326, in _verify_integrity
    raise construction_error(tot_items, block.shape[1:], self.axes)
ValueError: Shape of passed values is (4159, 11), indices imply (127, 11)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
lcp
  • 1
  • 1
  • Do a reset_index() for both the dataframes and try concatenating. – Raghul Raj Oct 01 '20 at 08:47
  • your error seems to disagree `ValueError: Shape of passed values is (4159, 11), indices imply (127, 11)` sounds like you need `merge`, `join` or `map` – Umar.H Oct 01 '20 at 08:47
  • Do you mean join like in SQL (which is usually done by `merge` in pandas) or append (in which case you need the same number of columns)? - Either way, this isn't concat. – Danny Varod Oct 01 '20 at 08:53
  • 1
    @LazyCoder, thanks a lot. Learned something new today. – lcp Oct 01 '20 at 13:42
  • @Danny Varod, thanks, got it working: pdat = pd.merge(df1, df2, left_index=True, right_index=True) – lcp Oct 01 '20 at 13:43
  • 1
    @Manakin, yeah, it took me some time to figure out. Thank you. – lcp Oct 01 '20 at 13:48

1 Answers1

-1

It looks like you need to use "join". Try this:

df1.join(df2,on="key_colname")

There are also arguments to set suffixes (lsuffix and rsuffix) and of course, how to join ("how" argument can be set to: left, right, outer, inner. By default it's left).