0

I have a question about pd.concat. I get some weird results and I do not get why.

Let start with a simple example (this should also show what I want to achieve):

import pandas as pd

df1 = pd.DataFrame([[1,2,3],[7,6,5]], columns = ["A","B","C"])
print("DF1: \n", df1)
df2 = pd.DataFrame([[4,5,6]], columns = ["A","B","C"])
print("DF2: \n", df2)
df3 = pd.concat([df1, df2], ignore_index = True)
print("Concat DF1 and DF2: \n",df3)

enter image description here

Now I have my actual programm where I have DataFrames like this: enter image description here

When I am applying the concat function, I get this:

enter image description here

It makes zero sense to me. What can possible be the reason?

P.S. It's not urgent, because I found a workaround but this bothers me and makes me a bit angry too.

3
  • 1
    Your question is unclear. What is your desired result, i.e., what do you specifically mean by "combining" two dataframes? Currently, you combine columns for each index (axis=1). You could also combine all rows for each column (axis=0). Feb 8 at 14:32
  • What is the expected result? Without that the question is unclear and could be closed... Feb 8 at 14:39
  • @harryhaller I want to make it one bigger DataFrame thus the smaller df is added at the end of the bigger df. Like here (what ironically works pretty fine): df1 = pd.DataFrame([[1,2,3],[7,6,5]], columns = ["A","B","C"]) df2 = pd.DataFrame([[4,5,6]], columns = ["A","B","C"]) df3 = pd.concat([df1, df2], ignore_index = True) I tried both axis 0 and 1 they both give me bs result. What ever the reason is, I really do not care to find out anymore. I found a solution. They're for sure something I overlooked, but honestly, I blame pandas for that.
    – Skobo Do
    Feb 8 at 14:40

2 Answers 2

0

Use the following code for connecting two DataFrame based on their rows

Code1) self.teste_df= (self.teste_df).append(test,ignore_index=True)
Code2) pd.concat([self.teste_df, test], axis = 0, ignore_index=True )
1
  • Yes I know, I tried it myself before asking the question. That should have worked, but it didn't. I updated the question. You can now see what the result of the Code2 would look like (the last image). That's the issue.
    – Skobo Do
    Feb 8 at 15:37
-2

I made them both a list, and combined the lists with +.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.