导入数据
import numpy as np
import pandas as pd
test_dict = {'id':[1, 2, 3, 4, 5], 'input':['5', '8', '3', '2', '9'],
'class_1':[0, 0, 1, 0, 1], 'class_2':[1, 0, 0, 1, 0],
'class_3':[0, 1, 0, 0, 0]}
#[1].直接写入参数test_dict
df = pd.DataFrame(test_dict)
df.head()
添加一列
# 在表格中添加一列
df['label'] = ''
df.head()
按条件打标签
df['label']=np.where(df['class_1']==1,0,df['label'])
df['label']=np.where(df['class_2']==1,1,df['label'])
df['label']=np.where(df['class_3']==1,2,df['label'])
df['label'] = df['label'].astype(int)
df.head()
删除离散标签列
df = df.drop(['class_1'], axis = 1)
df = df.drop(['class_2'], axis = 1)
df = df.drop(['class_3'], axis = 1)
df.head()