1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Aggregating data, and filtering on a date, in one block of code [closed]

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to aggregate sales data for the summer, for each customer. So I am summing up "sales", grouping by customer - and I'd like only date > 2020-07-01

    df = pd.DataFrame({
    "customer": [1,1,2,2,3,3],
    "sales": [56,69,32,2,45,90],
    "date": ['2020-09-01','2020-08-01','2020-07-01',
    '2020-08-01','2020-02-01','2020-01-01']})

    df['date'] = pd.to_datetime(df['date'])

    # this works successfully
    new_df = df.groupby("customer")["sales"].sum().reset_index()

    # this gives me an error
    new_df_summer = df.groupby("customer").filter(
    lambda x: x["date"] > "2020-06-01"
    ).groupby("customer")["sales"].sum().reset_index()


    I get this error: TypeError: filter function returned a Series, but expected a scalar bool

    Continue reading...

Compartilhe esta Página