Efficient way to assign values from another column pandas df

后端 未结 4 1539
慢半拍i
慢半拍i 2021-01-13 07:58

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

4条回答
  •  爱一瞬间的悲伤
    2021-01-13 08:20

    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
    

提交回复
热议问题