简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM