-1

I have 2 variables (dataframes) one is 47 colums wide and the other is 87, they are DF2 and DF2. Then I have a variable (dataframe) called full_data. Df1 and DF2 are two different subset of data I want to merge together once I find 2 rows are equal.

I am doing everything I want so far besides appending the right value to the new dataframe.

below is the line of code I have been playing around with:

full_data = full_data.append(pd.concat([df1[i:i+1].copy(),df2[j:j+1]].copy(), axis=1), ignore_index = True)

once I find the rows in both Df1 and DF2 are equal I am trying to read both those rows and put them one after the other as a single row in the variable full_data. What is happening right now is that the line of code is writting 2 rows and no one as I want.

what I want is full_data.append(Df1 DF2) and right now I am getting

full_data(i)=DF1
full_data(i+1)=DF2

Any help would be apreciated. EM

full_data = full_data.append(pd.concat([df1[i:i+1].copy(),df2[j:j+1]].copy(), axis=1), ignore_index = True)
Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • Welcome to SO. Please have a little tour on [how to ask a good question](https://stackoverflow.com/help/how-to-ask), also important to provide a [MRE](https://stackoverflow.com/help/minimal-reproducible-example). Please [edit](https://stackoverflow.com/posts/74586298/edit) your question with some example data and its desired output. – Rabinzel Nov 26 '22 at 23:19
  • Here is a good post with everything about [merging](https://stackoverflow.com/questions/53645882/pandas-merging-101) – Rabinzel Nov 26 '22 at 23:21
  • If both rows are **equal** - why do you want the rows one above the other - they will be the same ? Surely it would make more sense just to keep one of them ?? – ScottC Nov 27 '22 at 07:07
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 27 '22 at 18:37

1 Answers1

0

In the end I solved my problem. Probably I was not clear enough but my question but what was happening when concatenating is that I was getting duplicated or multiple rows when the expected result was getting a single row concatenation.

The issues was found to be with the indexing. Indexing had to be reset because of the way pandas works.

I found an example and explanation here

My solution here:

df3 = df2[j:j+1].copy()
df4 = df1[i:i+1].copy()

full_data = full_data.append(
    pd.concat([df4.reset_index(drop=True), df3.reset_index(drop=True)], axis=1),
    ignore_index = True
)

I first created a copy of my variables and then reset the indexes.

cafce25
  • 15,907
  • 4
  • 25
  • 31