代码之家  ›  专栏  ›  技术社区  ›  asmgx

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

  •  0
  • asmgx  · 技术社区  · 9 月前

    我有一个数据帧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
    

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

    2 回复  |  直到 9 月前
        1
  •  3
  •   Maria K    9 月前

    你快到了。尝试在中使用双括号 X_test[["RepID"]] 对于 dfCSV 作为一个数据帧,那么 dfCSV["Code"] = y_pred 以创建新列。

    以下是包含模拟数据的代码片段:

    X_test = pd.DataFrame({"RepID": [0, 1, 2], "Smth": [3, 4, 5]})
    y_pred = [5, 6, 7]
    
    dfCSV = X_test[["RepID"]]
    dfCSV["Code"] = y_pred
    print(dfCSV)
    

    输出:

       RepID  Code
    0      0     5
    1      1     6
    2      2     7
    
        2
  •  2
  •   taller    9 月前

    提供的代码演示了如何组合两列。我想这就是你要找的。

    import pandas as pd
    data_X = {
        'Weight': [18, 6, 21, 45, 30],
        'Height': [3, 0, 6, 6, 6],
        'Depth': [14, 6, 16, 20, 16],
        'RepID': [257428, 214932, 17675, 60819, 262530]
    }
    X_test = pd.DataFrame(data_X)
    data_y = {'Code': [0, 1, 2, 3, 4]}
    y_pred = pd.DataFrame(data_y)
    print(X_test)
    print(y_pred)
    dfCSV = pd.DataFrame([X_test["RepID"],y_pred['Code']]).T
    # Another option
    # dfCSV = pd.concat([X_test["RepID"], y_pred], axis=1)
    print(dfCSV)
    

    输出

       Weight  Height  Depth   RepID                                                              
    0      18       3     14  257428                                                              
    1       6       0      6  214932                                                              
    2      21       6     16   17675                                                              
    3      45       6     20   60819                                                              
    4      30       6     16  262530                                                              
       Code                                                                                       
    0     0                                                                                       
    1     1                                                                                       
    2     2                                                                                       
    3     3                                                                                       
    4     4                                                                                       
        RepID  Code                                                                               
    0  257428     0                                                                               
    1  214932     1                                                                               
    2   17675     2                                                                               
    3   60819     3                                                                               
    4  262530     4