在 python 中按周搜索

Search By week in python

大家好,我正在尝试构建一个查看 CSV 文件的函数,然后 return 向我发送特定一周的数据组。

    def searchWeek(week_Number):
  #dataLakeWeek = dataLake.groupby(dataLake.Week)
  week = dataLake.loc[dataLake["Week"] == f"{week_Number}","Agent":"atingimento"]
  week.groupby(dataLake['Week'])
  print(week)

 

所以我们的想法是 return 第一周内的所有数据按周分组,但也按代理分组。因此,如果 Guto R. 有不止一个数据条目,日期将添加

           day       Agent  Resolved  Meta  Week  Year
0 2021-01-04  Bárbara D.        95    52     1  2021
1 2021-01-04    Danielly        83    52     1  2021
2 2021-01-04     Guto R.       125    52     1  2021
3 2021-01-04  Helaine S.        19    52     1  2021
4 2021-01-04     João M.        94    52     1  2021
5 2021-01-04     Guto R.       125    52     1  2021

结果会是

           day       Agent  Resolved  Meta  Week  Year
10 2021-01-04     Guto R.       250    104    1  2021

使用groupby_agg:

out = df.groupby(['Week', 'Year', 'Agent'], as_index=False) \
        .agg({'day': 'first', 'Agent': 'first', 'Resolved': 'sum',
              'Meta': 'sum', 'Week': 'first', 'Year': 'first'})
print(out)

# Output
          day       Agent  Resolved  Meta  Week  Year
0  2021-01-04  Bárbara D.        95    52     1  2021
1  2021-01-04    Danielly        83    52     1  2021
2  2021-01-04     Guto R.       250   104     1  2021
3  2021-01-04  Helaine S.        19    52     1  2021
4  2021-01-04     João M.        94    52     1  2021