Search code examples
pythonpandasdatetimemergeconcatenation

pandas: Match data by months and days


How can I match data only by months and dates and not by year:

1. Data

2. Desired output


Solution

  • Convert Time column to datetimes and then to custom format MM/DD/ and then aggregate:

    df['Time'] = pd.to_datetime(df['Time']).dt.strftime('%m/%d/')
    
    df1 = df.groupby('Time').first()
    

    If need aggregate sum, mean if duplicates use:

    df2 = df.groupby('Time').sum(min_count=1)
    df3 = df.groupby('Time').mean()