您可以使用:
def f(x): #get unique days u = x['Day'].unique() #mapping dictionary d = dict(zip(u, np.arange(len(u)) // 3 + 1)) x['new'] = x['Day'].map(d) return xdf = df.groupby('Location', sort=False).apply(f)#add Location columns = df['new'].astype(str) + df['Location']#encoding by factorizedf['new'] = pd.Series(pd.factorize(s)[0] + 1).map(str).radd('C')print (df) Day Location new0 Mon Home C11 Tues Home C12 Wed Away C23 Wed Home C14 Thurs Away C25 Thurs Home C36 Fri Home C37 Mon Home C18 Sat Home C39 Fri Away C210 Sun Home C4
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)