【问题标题】:Join array to dataframe in python在python中将数组加入数据框
【发布时间】:2019-11-02 22:19:58
【问题描述】:

我正在做预测建模

像往常一样

将数据拆分成x_train、x_test、y_train、y_test

然后在 y_pred

中获取测试预测

完成后,我想将数据放入 csv 文件

但问题是当我尝试将 y_pred 加入 y_test 时,它没有按预期加入。

会得到类似的东西

    Class Data    TotalCnt  0
16  3     2209    5235      
98  3     2190    4871      
07  1     2183    1342      1690
09  1     2205    1540      1540
19  3     2191    4673      
01  1     2206    3117      1005
38  3     2200    4837      
44  3     2219    4965      
04  1     2195    1340      1690
10  1     2191    1980      2002
38  3     2184    4620      
15  3     2220    4781      
18  3     2223    4872      

它会删除一些记录

我认为问题的原因如下

y_pred 是来自原始数据帧的随机集的预测 所以它应该是这样的

ID      Prediction
16      1005
98      2056
07      1690
54      1690
...
.
.
.

y_pred 是一个数组,所以为了让我将它与 x_test 一起加入,我将它转换为数据帧

一旦 y_pred 转换为数据帧,它会丢失 ID,因此它变成连续的 1, 2, 3, 4, ...

ID      Prediction
1       1005
2       2056
3       1690
4       1690
...
.
.
.

因此,当尝试加入 x_test 时,它只匹配数据帧 x_testy_pred

中存在的 ID 号

我怎样才能将预测作为数据框而不是数组来获取

我正在使用这个

x_train, x_test, y_train, y_test = train_test_split(x,y)
rf = RandomForestRegressor(n_estimators=10000)
rf.fit(x_train, y_train) 
y_pred = rf.predict(x_test)

. . .
. . .

def Lead0(value):
        return "0" + str(value) if value < 10 else str(value)

dNow = datetime.datetime.now()
sNow = Lead0(dNow.year) + Lead0(dNow.month) + Lead0(dNow.day) + Lead0(dNow.hour) + Lead0(dNow.minute) + Lead0(dNow.second) 

y_pred = pd.DataFrame(y_pred)
y_out = x_test
y_out = y_out.join(y_test)
y_out = y_out.join(y_pred)

y_out.to_csv(sFolder + "dfPred__" + sNow +".csv")

如何在不丢失 ID 顺序的情况下将数组加入数据帧

如何在不丢失ID顺序的情况下将数组转换为数据框

【问题讨论】:

    标签: python dataframe


    【解决方案1】:

    y_pred 是来自原始数据帧的随机集的预测 y_pred 是一个数组

    我了解您希望保留原始数据帧中的索引

    为此,我认为您需要将旧数据框索引设为一列,然后将旧数据框系列 y_pred 保留为 dict 或数据框,而不是数组。

    import pandas as pd
    df = pd.DataFrame({'Record Type': ['100', '200', '300'],
               'Value': [(1,2,3,4,5), '0,10', 1]})
    
      Record Type            Value
    0         100  (1, 2, 3, 4, 5)
    1         200             0,10
    2         300                1
    

    然后将索引重置为列:

    df.reset_index(level=0, inplace=True)
    
       index Record Type            Value
    0      0         100  (1, 2, 3, 4, 5)
    1      1         200             0,10
    2      2         300                1
    

    现在您可以保留旧数据帧中的索引(现在是常规系列)和 y_pred 值,并将其与新数据帧合并。

    要将新的 df 与旧的合并,请使用合并:

    import pandas as pd
    
    df1 = pd.DataFrame({'Record Type': ['100', '200', '300'],
               'Value': [(1,2,3,4,5), '0,10', 1]})
    
    df1.reset_index(level=0, inplace=True)
    
    df2 = pd.DataFrame({'Record Type': ['100', '200', '300'],
               'Value': [(1,2,3,4,5), '0,10', 1]})
    
    df2.reset_index(level=0, inplace=True)
    
    
    # to merge dataframes on column index
    df_all = df1.merge(df2, on='index', indicator = True) #indicator show 
                                # if record was found in one df or both
    
    df_all.columns #show column list
    df_all = df_all[['index','Record Type_y','Value_y']] #pick only columns you want
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2022-01-01
    • 2020-03-18
    • 2018-12-11
    • 2015-04-22
    相关资源
    最近更新 更多