I\'m trying to create a more efficient script that creates a new column
based off values in another column. The script below performs this but I can only select
You can use:
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 x
df = df.groupby('Location', sort=False).apply(f)
#add Location column
s = df['new'].astype(str) + df['Location']
#encoding by factorize
df['new'] = pd.Series(pd.factorize(s)[0] + 1).map(str).radd('C')
print (df)
Day Location new
0 Mon Home C1
1 Tues Home C1
2 Wed Away C2
3 Wed Home C1
4 Thurs Away C2
5 Thurs Home C3
6 Fri Home C3
7 Mon Home C1
8 Sat Home C3
9 Fri Away C2
10 Sun Home C4