Pandas: pd.concat does not work correctly

Created on 17 Feb 2019  路  6Comments  路  Source: pandas-dev/pandas

Code Sample, a copy-pastable example if possible

df1.shape    # (21141, 59)
df2.shape    # (21141, 6)
result = pd.concat([df1, df2], axis=1, ignore_index=True)
result.shape    # (42282, 65)

Problem description

I have 2 dataframes that I try to concatenate horizontally. The method concat doesn't work: it returns a dataframe with a wrong dimension. Moreover, all column names happen to be changed to numbers going from 0 to 64...

The dataframes are created from a dataset that is a bit big so I cannot reproduce the creation code here but I can provide you with more details by e-mail.

Expected Output

The right dimension should be (21141, 65) and the resulting columns should be just the concatenation of df1's columns and df2's columns.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel
byteorder: little
LC_ALL: None
LANG: fr
LOCALE: None.None

pandas: 0.22.0
pytest: 3.3.2
pip: 18.0
setuptools: 39.0.1
Cython: 0.27.3
numpy: 1.14.2
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Most helpful comment

It's hard to say without a minimal example, but it appears that you're getting confused by the alignment. See http://pandas-docs.github.io/pandas-docs-travis/reference/api/pandas.concat.html?highlight=concat#pandas.concat

Specifically, the ignore_index_parameter

Note the index values on the other axes are still respected in the join.

Since you're using axis=1,

  1. the column labels will be reset to [0, n)
  2. the row labels will be preserved (and aligned).

If you really don't care about your row labels, then you'll want to drop the row labels before concating pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], ...)

All 6 comments

It's hard to say without a minimal example, but it appears that you're getting confused by the alignment. See http://pandas-docs.github.io/pandas-docs-travis/reference/api/pandas.concat.html?highlight=concat#pandas.concat

Specifically, the ignore_index_parameter

Note the index values on the other axes are still respected in the join.

Since you're using axis=1,

  1. the column labels will be reset to [0, n)
  2. the row labels will be preserved (and aligned).

If you really don't care about your row labels, then you'll want to drop the row labels before concating pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], ...)

Ok, I didn't get that ignore_index_parameter was referring to the concatenation axis index. But I don't understand its utility in that case. The most classic use case happening in data preparation is adding to a dataframe the columns of another dataframe (given that they have of course the same number of rows), whatever their row indices. I regret that ignore_index_parameter would not allow that.

If you really don't care about your row labels, then you'll want to drop the row labels before concating pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], {{axis=1 - added by mirekphd}}...)

This is arguably whaty ignore_index=Trueshould be doing instead of failing silently.

I agree with @Mark531 there should be an intuitive manner to merge dataframes horizontally. the documentation on ignore_index=True is unclear, I also spent time on this.

I agree that this is not that clear. it took me quite a while to figure out what was going on. In the end this worked great
pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], ...)

It's hard to say without a minimal example, but it appears that you're getting confused by the alignment. See http://pandas-docs.github.io/pandas-docs-travis/reference/api/pandas.concat.html?highlight=concat#pandas.concat

Specifically, the ignore_index_parameter

Note the index values on the other axes are still respected in the join.

Since you're using axis=1,

  1. the column labels will be reset to [0, n)
  2. the row labels will be preserved (and aligned).

If you really don't care about your row labels, then you'll want to drop the row labels before concating pd.concat([df1.reset_index(drop=True), df2.reset_index(drop=True)], ...)

Hey! Had been having the same problem for many days. Couldn't find a solution that worked until I found this. Thank you for explaining it so well!

Was this page helpful?
0 / 5 - 0 ratings