Pandas version of rbind

前端 未结 4 730
野的像风
野的像风 2021-02-01 12:03

In R, you can combine two dataframes by sticking the columns of one onto the bottom of the columns of the other using rbind. In pandas, how do you accomplish the same thing? It

4条回答
  •  旧时难觅i
    2021-02-01 12:24

    pd.concat will serve the purpose of rbind in R.

    import pandas as pd
    df1 = pd.DataFrame({'col1': [1,2], 'col2':[3,4]})
    df2 = pd.DataFrame({'col1': [5,6], 'col2':[7,8]})
    print(df1)
    print(df2)
    print(pd.concat([df1, df2]))
    

    The outcome will looks like:

       col1  col2
    0     1     3
    1     2     4
       col1  col2
    0     5     7
    1     6     8
       col1  col2
    0     1     3
    1     2     4
    0     5     7
    1     6     8
    

    If you read the documentation careful enough, it will also explain other operations like cbind, ..etc.

提交回复
热议问题