Search code examples
pythonpandasdataframeindexingexpand

Make index first row in group in pandas dataframe


I was wondering if it were possible to make the first row of each group based on index, the name of that index. Suppose we have a df like this:

dic = {'index_col': ['a','a','a','b','b','b'],'col1': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(dic).set_index('index_col')

df1

Is it possible to transform the dataframe above to one that looks like the one below? What happened here is that the index has been reset, and for every group, the first row is the index name?

df2


Solution

  • The result is a pandas.Series;

    df_list = []
    for label , group in df.groupby('index_col'):
        df_list.append(pandas.concat([pandas.Series([label]), group['col1']]))
    
    df_result = pandas.concat(df_list).reset_index(drop=True)
    

    Output;

    0    a
    1    1
    2    2
    3    3
    4    b
    5    4
    6    5
    7    6
    dtype: object
    

    Call df_result.to_frame() if you want a data-frame.