本篇文章小编给大家分享一下python实现添加标签&打标签操作代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
普通打标签
odue_df=df_train_stmt.loc[(df_train_stmt.AGE3>0)|(df_train_stmt.AGE4>0)|(df_train_stmt.AGE5>0)|(df_train_stmt.AGE6>0),['XACCOUNT']].drop_duplicates() odue_df['label']=1 cust_df=df_acct[['CUSTR_NBR','XACCOUNT']].drop_duplicates() #做合并 df_y=pd.merge(cust_df,odue_df,how='left',on='XACCOUNT').groupby('CUSTR_NBR').agg({'label':max}).reset_index().fillna(0)
使用函数来打标签
#标注标签 Label def label(row): if row['Date_received'] == 'null': return -1 if row['Date'] != 'null': td = pd.to_datetime(row['Date'], format='%Y%m%d') - pd.to_datetime(row['Date_received'], format='%Y%m%d') if td <= pd.Timedelta(15, 'D'): return 1 return 0 dfoff['label'] = dfoff.apply(label, axis=1) #打标签,判断天数 def get_label(s): s = s.split(':') if s[0]=='null': return 0 elif (date(int(s[0][0:4]),int(s[0][4:6]),int(s[0][6:8]))-date(int(s[1][0:4]),int(s[1][4:6]),int(s[1][6:8]))).days<=15: return 1 else: return -1 dataset2.label = dataset2.label.apply(get_label)