Search code examples
pythonpandas-groupby

build a incremental list and reset it when condition changes - pivot it at the end


I am trying to create an incremental list based on a group and reset it every time the group changes. After that, I would like to pivot that table, and use that incremental list as the index

table = [['marco', 5, 'brasil','2022-05-01'],
         ['isa', 8, 'eua','2022-05-02'],
         ['maria', 5, 'chile','2022-05-03'],
         ['fernanda', 8, 'brasil','2022-05-01'],
         ['marco', 5, 'chile','2022-05-02']]

df = pd.DataFrame(table, columns = ['Name', 'Age', 'Country', 'Birthday'])
df.set_index('Birthday', inplace=True)
df.sort_values('Birthday', axis=0, ascending=True)

df['count']= df.groupby(df['Birthday'].cumsum()).cumcount()

Current output

Desired output

Desired output

The final result should be a pivot table using

df_pivot = df.pivot(index='count', columns='Birthday', values='Name')

Final result


Solution

  • df=df.sort_values('Birthday', axis=0, ascending=True)
    df['Month']=df['Birthday'].apply(lambda x: x[6:]) # get month and use group by on this
    
    df['count']=(df.groupby('Month').cumcount())+1
    
    df.pivot(index='count', columns='Birthday', values='Name')
    

    I couldn't get the counter to automatically work, I used cumcount())+1