Pandas recalculate index after a concatenation

前端 未结 3 1698
盖世英雄少女心
盖世英雄少女心 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:13

    This should work:

    train_df.reset_index(inplace=True, drop=True) 
    

    Set drop to True to avoid an additional column in your dataframe.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-04 22:35

    If your index is autogenerated and you don't want to keep it, you can use the ignore_index option. `

    train_df = pd.concat(train_class_df_list, ignore_index=True)
    

    This will autogenerate a new index for you, and my guess is that this is exactly what you are after.

    0 讨论(0)
提交回复
热议问题