SQL_M
SQL_M

Reputation: 2475

Append two dataframes (Python Pandas)

Very basic issue, but I'm having a hard time figuring out what's wrong. As a result of a Logistic Regression model, I have a set of predictions. I turned that numpy array into a dataframe (single column). Now I want to 'attach' the results to my original data, so I can check my predictions. Seems so simple, but I get the following message:

RuntimeWarning: '<' not supported between instances of 'str' and 'int', sort order is undefined for incomparable objects result = result.union(other)

Sample Looks like this:

Set
ColA, ColB, ColC
1234, AAAA, BBBB

Pred
0 -- column name
1 -- values

Desired result:
ColA, ColB, ColC, 0
1234, AAAA, BBBB, 1

Actual result:
ColA, ColB, ColC, NaN
NaN,  NaN,  NaN,  1

My code seems straightforward:

res = Set.append(Pred)
res

Any help with this issue would be great. As you might see, I am at a beginner level in Pandas...

Regards, M.

Upvotes: 1

Views: 144

Answers (1)

jezrael
jezrael

Reputation: 862471

First create default indices:

Set = Set.reset_index(drop=True)
Pred = Pred.reset_index(drop=True)

Then use DataFrame.join:

res = Set.join(Pred)

Or pandas.concat:

res = pd.concat([Set, Pred], axis=1)

Or assign if Pred is Series:

res = Set.assign(Pred=Pred)

Upvotes: 1

Related Questions