Search code examples
pythonsearchpandas-groupbyweek-number

Search By week in python


Hi everyone I'm trying to build a function that looks into a CSV file and returns me the group of data in a particular week.

    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)

 

So the idea would be to return all the data within week one grouped by week but by agents also. So if Guto R. has more then one data entry the date would add

           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

Result would be

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

Solution

  • Use 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