我有以下数据框。我想要实现的是,对于每个“已完成日期”的工作,“时间已花费”列中的小时总和应等于7。例如,在6/10/2019,小时总和已为7,因此不需要进行调整。在2019年6月12日,小时总和为4.25,因此我需要插入带有“ Tab_description”“ Difference”的行,该行在“ Time_spent”下的差异为2.75。 6/13/2019和6/14/2019已经达到7,因此在那里无需执行任何操作。对于6/19/2019,我需要执行与6/12/2019相同的操作,插入总和为6的行,以使总和为7。
Date_worked Tab_description Time_spent
0 6/10/2019 Perform planning procedures 7.0
1 6/11/2019 Perform planning procedures 7.0
2 6/12/2019 Time off (away from the office) 2.25
3 6/12/2019 Staff meeting 1.0
4 6/12/2019 Accounting & Risk Management Luncheon 1.0
5 6/13/2019 Perform planning procedures 7.0
6 6/14/2019 Time off (away from the office) 2.0
7 6/14/2019 Review policies and procedures 5.0
8 6/17/2019 Time off (away from the office) 7.0
9 6/18/2019 Perform planning procedures 7.0
10 6/19/2019 Staff meeting 1.0
11 6/20/2019 Time off (away from the office) 2.0
12 6/21/2019 Time off (away from the office) 1.0
13 6/24/2019 Staff meeting (FY 20 planning) 7.0
14 6/25/2019 FCR Kick-off meeting 1.0
15 6/26/2019 Time off (away from the office) 1.5
16 6/26/2019 Staff meeting 1.0
17 6/28/2019 Time off (away from the office) 1.0
答案 0 :(得分:1)
有很多方法可以做到这一点,我将使用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