Efficient way to assign values from another column pandas df

后端 未结 4 1538
慢半拍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:32

    # DataFrame Given
    df = pd.DataFrame({
        'Day' : ['Mon','Tues','Mon','Wed','Thurs','Fri','Mon','Sat','Sun','Tues'],                 
        'Location' : ['Home','Home','Away','Home','Home','Home','Home','Home','Home','Away'],                   
         })
    Unique_group = ['Mon','Tues','Wed']
    df['Group'] = df['Day'].apply(lambda x:1 if x in Unique_group else 2)
    df['Assign'] = np.zeros(len(df))
    # Assigning the ditionary values for output from numeric
    vals = dict([(i,'C'+str(i)) for i in range(len(df))])
    

    Loop to cut the dataframe for each line and checking the previous 'Assign' column info to assign new value

    for i in range(1,len(df)+1,1):
        # Slicing the Dataframe line by line
        df1 = df[:i]
        # Incorporating the conditions of Group and Location
        df1 = df1[(df1.Location == df1.Location.loc[i-1]) & (df1.Group == df1.Group.loc[i-1]) ]
        # Writing the 'Assign' value for the first line of sliced df
        if len(df1)==1:
            df.loc[i-1,'Assign'] = df[:i].Assign.max()+1
        # Writing the 'Assign value based on previous values if it has contiuos 2 values of same group
        elif (df1.Assign.value_counts()[df1.Assign.max()] <3):
            df.loc[i-1,'Assign'] = df1.Assign.max()
        # Writing 'Assign' value for new group
        else:
            df.loc[i-1,'Assign'] = df[:i]['Assign'].max()+1
    df.Assign = df.Assign.map(vals)
    

    Out:

         Day    Location    Group   Assign
    0   Mon Home    1   C1
    1   Tues    Home    1   C1
    2   Mon Away    1   C2
    3   Wed Home    1   C1
    4   Thurs   Home    2   C3
    5   Fri Home    2   C3
    6   Mon Home    1   C4
    7   Sat Home    2   C3
    8   Sun Home    2   C5
    9   Tues    Away    1   C2
    

提交回复
热议问题