how to crate the group by in pandas only in one level

后端 未结 3 1853
忘掉有多难
忘掉有多难 2021-01-16 01:04

I am importing below df3 dataframe in my excel file and want to grouby only Name and rest dublicate data should reflect as below .

Note (Each Month data will be added

3条回答
  •  忘掉有多难
    2021-01-16 01:26

    Code

    #creating sample data as per requirement
    import pandas as pd 
    df = pd.DataFrame({'Name':['Jon','Jon','Jon','Mike','Mike','Jon','Jon'],'ID':[1,1,1,1,1,1,1], 'Month':['Feb','Jan','Mar','Jan','Jan','Feb','Jan'], 'Shift':['A','B','C','A','B','C','A']})
    #display data
    df
    

    df['Month'] = pd.Categorical(df['Month'],categories=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],ordered=True)
    df = df.sort_values(['Name','Month']).reset_index(drop=True)
    #display final data
    df
    

    I hope this would be helpful... : )

提交回复
热议问题