go-gota / gota

Gota: DataFrames and data wrangling in Go (Golang)
Other
2.98k stars 276 forks source link

[Question] Concat by column #144

Closed pradithya closed 2 years ago

pradithya commented 3 years ago

Is there a way to concatenate dataframe into another column-wise?

e.g.

dataframe 1: col1 col2
11 21
12 22
dataframe 2: col3 col4
31 41
32 42

Result:

col1 col2 col3 col4
11 21 31 41
12 22 32 42
prliu commented 3 years ago

Hello @pradithya,

I believe following code will solve your question...

package main

import (
    "fmt"

    "github.com/go-gota/gota/dataframe"
)

func main() {
    rec1 := [][]string{
        {"Col1", "Col2"},
        {"11", "21"},
        {"12", "22"},
    }

    rec2 := [][]string{
        {"Col3", "Col4"},
        {"31", "41"},
        {"32", "42"},
    }

    df1 := dataframe.LoadRecords(rec1)
    df2 := dataframe.LoadRecords(rec2)

    fmt.Println(df1)
    fmt.Println(df2)

    dd := df1.CBind(df2)
    fmt.Println(dd)
}

Result:

[2x2] DataFrame

    Col1  Col2 
 0: 11    21   
 1: 12    22   
    <int> <int>

[2x2] DataFrame

    Col3  Col4 
 0: 31    41   
 1: 32    42   
    <int> <int>

[2x4] DataFrame

    Col1  Col2  Col3  Col4 
 0: 11    21    31    41   
 1: 12    22    32    42   
    <int> <int> <int> <int>