此列表理解中的递增数字

时间:2018-09-26 19:56:03

标签: python pandas

请考虑以下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
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]

这很好,但是我需要为每个重复项增加“周数”,以便约翰C在1922、1923和1924的星期中有3行。

我尝试过

[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 个答案:

答案 0 :(得分:1)

如果我的理解正确,您可以只创建要复制的行的辅助数据框,然后在该辅助数据框上增加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

答案 1 :(得分:1)

可以传递,它是一个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