代码之家  ›  专栏  ›  技术社区  ›  Umar.H

此列表中的增量编号

  •  0
  • Umar.H  · 技术社区  · 6 年前

       ID   Name    Week    Course        Hours
    0   1   John A  1922    Bike Tech     5.5
    1   2   John B  1922    Auto Tech     3.2
    2   3   John C  1922    Prison        3.5
    3   4   John D  1922    Comp          6.5
    4   5   John E  1922    Awareness     7.0
    5   6   John F  1922    First Aid     7.2
    6   7   John G  1922    BasketBall    2.5
    7   8   John H  1922    Tech          5.4
    

    我使用以下代码复制行

    duplicate = [3 if val == 'Prison' else 1 for val in df.Course]
    

    这很好,但是我需要为每一个副本增加周数,这样johnc就有3行,分别是1922周、1923周和1924周。

    我试过了

    [3 if val == 'Prison' and df.Week +1 else 1 for val in df.Course]
    

      ID   Name    Week    Course        Hours
    0   1   John A  1922    Bike Tech     5.5
    1   2   John B  1922    Auto Tech     3.2
    2   3   John C  1922    Prison        3.5
    2   3   John C  1923    Prison        3.5
    2   3   John C  1924    Prison        3.5
    3   4   John D  1922    Comp          6.5
    4   5   John E  1922    Awareness     7.0
    5   6   John F  1922    First Aid     7.2
    6   7   John G  1922    BasketBall    2.5
    7   8   John H  1922    Tech          5.4 
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   sacuL    6 年前

    Week 编号,然后连接到原始数据帧:

    helper = pd.concat([df.loc[df.Course == 'Prison']]*2)
    helper['Week'] += helper.reset_index().index+1
    
    df = pd.concat((df,helper)).sort_values('ID')
    
    >>> df
       ID    Name  Week      Course  Hours
    0   1  John A  1922   Bike Tech    5.5
    1   2  John B  1922   Auto Tech    3.2
    2   3  John C  1922      Prison    3.5
    2   3  John C  1923      Prison    3.5
    2   3  John C  1924      Prison    3.5
    3   4  John D  1922        Comp    6.5
    4   5  John E  1922   Awareness    7.0
    5   6  John F  1922   First Aid    7.2
    6   7  John G  1922  BasketBall    2.5
    7   8  John H  1922        Tech    5.4
    
        2
  •  1
  •   rafaelc    6 年前

    可以通过 ,这是一个 pd.Series 价值观与你的 df

    >>> row = df.loc[df.Course.eq('Prison'), :].iloc[0,:].copy()
    
    ID             3
    Name      John C
    Week        1922
    Course    Prison
    Hours        3.5
    Name: 2, dtype: object
    

    那么

    def duplicate(n, row, df):
        week = row['Week']
        for i in range(1, n+1):
            row['Week'] = week + i
            df.loc[-i, :] = row
        return df.sort_values('ID').reset_index(drop=True)
    
    
    >>> duplicate(3, row, df )
    
        ID  Name    Week    Course      Hours
    0   1.0 John A  1922.0  Bike Tech   5.5
    1   2.0 John B  1922.0  Auto Tech   3.2
    2   3.0 John C  1922.0  Prison      3.5
    3   3.0 John C  1923.0  Prison      3.5
    4   3.0 John C  1924.0  Prison      3.5
    5   3.0 John C  1925.0  Prison      3.5
    6   4.0 John D  1922.0  Comp        6.5
    7   5.0 John E  1922.0  Awareness   7.0
    8   6.0 John F  1922.0  First Aid   7.2
    9   7.0 John G  1922.0  BasketBall  2.5
    10  8.0 John H  1922.0  Tech        5.4