Robert Penridge
Robert Penridge

Reputation: 8513

Trouble with ignore_index and concat()

I'm new to Python. I have 2 dataframes each with a single column. I want to join them together and keep the values based on their respective positions in each of the tables.

My code looks something like this:

huh = pd.DataFrame(columns=['result'], data=['a','b','c','d'])
huh2 = pd.DataFrame(columns=['result2'], data=['aa','bb','cc','dd'])
huh2 = huh2.sort_values('result2', ascending=False)
tmp = pd.concat([huh,huh2], ignore_index=True, axis=1)
tmp

From the documentation it looks like the ignore_index flag and axis=1 should be sufficient to achieve this but the results obviously disagree.

Current Output:

    0   1
0   a   aa
1   b   bb
2   c   cc
3   d   dd

Desired Output:

    result  result2
0   a       dd
1   b       cc
2   c       bb
3   d       aa

Upvotes: 2

Views: 2773

Answers (1)

cs95
cs95

Reputation: 402523

If you concatenate the DataFrames horizontally, then the column names are ignored. If you concatenate vertically, the indexes are ignored. You can only ignore one or the other, not both.

In your case, I would recommend setting the index of "huh2" to be the same as that of "huh".

pd.concat([huh, huh2.set_index(huh.index)], axis=1)

  result result2
0      a      dd
1      b      cc
2      c      bb
3      d      aa

If you aren't dealing with custom indices, reset_index will suffice.

pd.concat([huh, huh2.reset_index(drop=True)], axis=1)

  result result2
0      a      dd
1      b      cc
2      c      bb
3      d      aa

Upvotes: 4

Related Questions