将一列热编码数据添加到数据框

时间:2019-09-11 10:09:21

标签: python pandas dataframe

df ==多个用户数据的位置点

    tslot             user location point  
0   2015-12-04 13:00:00 0   4356           
1   2015-12-04 13:15:00 0   4356           
2   2015-12-04 13:30:00 0   4356          
3   2015-12-04 13:45:00 0   4356          
4   2015-12-04 14:00:00 0   4356         
5   2015-12-04 14:15:00 0   4356         
6   2015-12-04 14:30:00 0   4356          
7   2015-12-04 14:45:00 0   4356          
8   2015-12-04 15:00:00 0   7645         

我需要创建一列编码数据。

data = pd.concat([df, pd.DataFrame(encoding(df))], axis=1)  #encoding is onehotencoding

给我多列

         tslot           user   loaction_point  0   1   2   3   4   5   6   ... 926 927 928 929 930 931 932 933 934 935
0   2015-12-04 13:00:00     0   4356            0.0 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

但我需要

    tslot             user location point  endoded
0   2015-12-04 13:00:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
1   2015-12-04 13:15:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
2   2015-12-04 13:30:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
3   2015-12-04 13:45:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
4   2015-12-04 14:00:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
5   2015-12-04 14:15:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
6   2015-12-04 14:30:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
7   2015-12-04 14:45:00 0   4356           [0., 0., 0., ..., 0., 0., 0.]
8   2015-12-04 15:00:00 0   7645           [0., 0., 0., ..., 0., 0., 0.]

2 个答案:

答案 0 :(得分:1)

我希望你需要

df['encoded'] = encoding(df).tolist()

答案 1 :(得分:0)

希望这会有所帮助:

df['encoded'] = df.applymap(lambda x: np.array(pd.DataFrame(encoding(x))))