Есть много способов сделать это, я покажу вам, используя groupby
& concat
.
Сначала позвольте нам вычислить общее время и разницу,
print(df)
Date_worked Tab_description Time_spent
0 6/10/2019 Perform planning procedures 7.00
1 6/11/2019 Perform planning procedures 7.00
2 6/12/2019 Time off (away from the office) 0.25
3 6/12/2019 Staff meeting 1.00
4 6/12/2019 Accounting & Risk Management Luncheon 1.00
5 6/13/2019 Perform planning procedures 7.00
6 6/14/2019 Time off (away from the office) 2.00
7 6/14/2019 Review policies and procedures 5.00
8 6/17/2019 Time off (away from the office) 7.00
9 6/18/2019 Perform planning procedures 7.00
10 6/19/2019 Staff meeting 1.00
11 6/20/2019 Time off (away from the office) 2.00
12 6/21/2019 Time off (away from the office) 1.00
13 6/24/2019 Staff meeting (FY 7.00
14 6/25/2019 FCR Kick-off meeting 1.00
15 6/26/2019 Time off (away from the office) 1.50
16 6/26/2019 Staff meeting 1.00
17 6/28/2019 Time off (away from the office) 1.00
мы начинаем с groupby
и простой разностной суммы, мы назначаем ее новой переменной с именем df2.
df2 = df.groupby('Date_worked')['Time_spent'].sum().reset_index()
df2['variance'] = df2['Time_spent'] - 7.00
Теперь мы создаем столбец вкладки и создаем запрошенное вами описание,
df2.loc[df2['variance'] != 0, 'Tab_description'] = 'Difference'
Затем мы отбрасываем любые строки NaN, отбрасываем столбец 'Time_spent'
и переименовываем столбец «Дисперсия» в временную задержку в пределах concat
.
pd.concat(
[
df,
df2.dropna()
.drop("Time_spent", axis=1)
.rename(columns={"variance": "Time_spent"}),
],
sort=False,
)
print(df)
Date_worked Tab_description Time_spent
0 6/10/2019 Perform planning procedures 7.00
1 6/11/2019 Perform planning procedures 7.00
2 6/12/2019 Time off (away from the office) 0.25
3 6/12/2019 Staff meeting 1.00
4 6/12/2019 Accounting & Risk Management Luncheon 1.00
5 6/13/2019 Perform planning procedures 7.00
6 6/14/2019 Time off (away from the office) 2.00
7 6/14/2019 Review policies and procedures 5.00
8 6/17/2019 Time off (away from the office) 7.00
9 6/18/2019 Perform planning procedures 7.00
10 6/19/2019 Staff meeting 1.00
11 6/20/2019 Time off (away from the office) 2.00
12 6/21/2019 Time off (away from the office) 1.00
13 6/24/2019 Staff meeting (FY 7.00
14 6/25/2019 FCR Kick-off meeting 1.00
15 6/26/2019 Time off (away from the office) 1.50
16 6/26/2019 Staff meeting 1.00
17 6/28/2019 Time off (away from the office) 1.00
2 6/12/2019 Difference -4.75
7 6/19/2019 Difference -6.00
8 6/20/2019 Difference -5.00
9 6/21/2019 Difference -6.00
11 6/25/2019 Difference -6.00
12 6/26/2019 Difference -4.50
13 6/28/2019 Difference -6.00