Pandas recalculate index after a concatenation

前端 未结 3 1714
盖世英雄少女心
盖世英雄少女心 2020-12-04 22:01

I have a problem where I produce a pandas dataframe by concatenating along the row axis (stacking vertically).

Each of the constituent dataframes has an autogenerat

3条回答
  •  余生分开走
    2020-12-04 22:27

    After vertical concatenation, if you get an index of [0, n) followed by [0, m), all you need to do is call reset_index:

    train_df.reset_index(drop=True)
    

    (you can do this in place using inplace=True).


    import pandas as pd
    
    >>> pd.concat([
        pd.DataFrame({'a': [1, 2]}), 
        pd.DataFrame({'a': [1, 2]})]).reset_index(drop=True)
        a
    0   1
    1   2
    2   1
    3   2
    

提交回复
热议问题