Py学习  »  Python

为什么合并数据帧不能按照python中的预期方式工作

asmgx • 1 年前 • 978 次点击  

我有一个数据帧df

看起来像这样

            Weight    Height    Depth RepID       Code
0           18         3        14    257428      0
1            6         0         6    214932      0
2           21         6        16     17675      0
3           45         6        20     60819      0
4           30         6        16    262530      0
       ...       ...       ...       ...    ...
4223        36         6        28    331596      1
4224        24         9         0    331597      1
4225        36        12         8    331632      1
4226        24        24         0    331633      1
4227        30         9         0    331634      1

[4228 rows x 5 columns]

我在测试和训练数据集中对其进行了分解

y = df["Code"]
X = df.drop("Code", axis=1, errors='ignore')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=TestSize, random_state=56)

然后预测这些值

    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

现在,我想将预测结果及其相关的RepID保存在一个文件中

所以我做了这个

   dfCSV = X_test["RepID"]
    dfCSV["Code"] = pd.DataFrame(y_pred)
    dfCSV.to_csv(PredictionFile)

预期结束的数据帧如下

         RepID       Code
0        84833      0
1        38388      1
2         2848      0
3         2992      1
4        28279      0
       ....    ...
423     74993      1
424     39924      1
425     55339      0
426     33882      1
427     64490      1

但结果是第一次看到的

dfCSV
Out[15]: 
3792                                                262578
482                                                 129648
62                                                    7144
2998                                                127711
840                                                 157391
                       
207                                                 277899
569                                                  89965
2895                                                116296
570                                                 279183
ICD10         0
0    1
1    1
2    0
3    1
4    0
..  ...
Name: RepID, Length: 847, dtype: object

发生了什么以及如何修复?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/161273
 
978 次点击  
文章 [ 2 ]  |  最新文章 1 年前