1

I have the following dataframes in python pandas:

A:

      1   2   3   4   5   6   7   8   9  10
0 1   1   1   1   1   1   1   0   0   1   1

B:

      1   2   3   4   5   6   7   8   9  10
1 0   1   1   1   1   1   1   0   0   1   0

C:

      1   2   3   4   5   6   7   8   9  10
2 0   1   1   1   0   0   0   0   0   1   0

I want to concatenate them together such that the column titles remain the same while row index and values get appended so the new dataframe is:

df:

      1   2   3   4   5   6   7   8   9  10
0 1   1   1   1   1   1   1   0   0   1   1
1 0   1   1   1   1   1   1   0   0   1   0
2 0   1   1   1   0   0   0   0   0   1   0

I have tried using append and concat but none seem to be fulfilling the output I am trying to achieve. Any suggestions?

Here is what I tried:

df = pd.concat([df,pd.concat([A,B,C], ignore_index=True)], axis=1)
2
  • 1
    This is a plain vanilla concat pd.concat([A, B, C]) Commented Feb 16, 2018 at 0:27
  • Ah yes @piRSquared Thats right! Answer it so I can mark as correct. The more complicated approaches didn't work, it did! Commented Feb 16, 2018 at 0:35

2 Answers 2

3

This is a plain vanilla concat

pd.concat([A, B, C])

     1  2  3  4  5  6  7  8  9  10
0 1  1  1  1  1  1  1  0  0  1   1
1 0  1  1  1  1  1  1  0  0  1   0
2 0  1  1  1  0  0  0  0  0  1   0
Sign up to request clarification or add additional context in comments.

Comments

2

Simple pd.concat will just do the work, you over complicated the task a little bit:

pd.concat([A,B,C], axis=0, ignore_index=True)

2 Comments

For this problem, the last 2 params are redundant, but props for being explicit :)
If B and C do have the correct index, then it is redundant, but I always want it to be clear when I'm joining. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.