Search code examples
pythonpandasdataframeindexingsklearn-pandas

how to fill rows with help of index pandas?


how to fill rows with help of index pandas?

I have dataframe like

    Alert number    Age Job Loan
0        0          58   4   0
2        2          44   9   0
4        4          35   4   0
6        6          41   0   0
8        8          29   0   0

I have another dataframe with same no of columns and different index values like

     Alert number   Age Job Loan
1        1          58   4   0
3        3          44   9   0
5        5          35   4   0
7        7          41   0   0
9        9          29   0   0

How to combine the above two rows sequentially in pandas?

Like the below dataframe

    Alert number    Age Job Loan
0        0          58   4   0
1        1          58   4   0
2        2          44   9   0
3        3          44   9   0
4        4          35   4   0
5        5          35   4   0
6        6          41   0   0
7        7          41   0   0
8        8          29   0   0
9        9          29   0   0

Solution

  • Use concat with sorting by DataFrame.sort_values:

    df = pd.concat([df1, df2]).sort_values('Alert number', ignore_index=True)
    

    Or by DataFrame.sort_index:

    df = pd.concat([df1, df2]).sort_index()