diff --git a/platform-engineer/go-dataframes/analysis.csv b/platform-engineer/go-dataframes/analysis.csv new file mode 100755 index 0000000..eff6d9c --- /dev/null +++ b/platform-engineer/go-dataframes/analysis.csv @@ -0,0 +1,3 @@ +group_id +1 +2 diff --git a/platform-engineer/go-dataframes/data.csv b/platform-engineer/go-dataframes/data.csv new file mode 100644 index 0000000..fce7039 --- /dev/null +++ b/platform-engineer/go-dataframes/data.csv @@ -0,0 +1,9 @@ +group_id,individual_id,attr_1,attr_2 +1,1,abc,def +1,2,abc,def +1,3,abc,def +1,4,abc,def +2,5,abc,def +2,6,abc,def +2,7,abc,def +2,8,abc,def diff --git a/platform-engineer/go-dataframes/go.mod b/platform-engineer/go-dataframes/go.mod new file mode 100644 index 0000000..e56c60a --- /dev/null +++ b/platform-engineer/go-dataframes/go.mod @@ -0,0 +1,5 @@ +module live_coding + +go 1.16 + +require github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8 diff --git a/platform-engineer/go-dataframes/go.sum b/platform-engineer/go-dataframes/go.sum new file mode 100644 index 0000000..75acb28 --- /dev/null +++ b/platform-engineer/go-dataframes/go.sum @@ -0,0 +1,2 @@ +github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8 h1:hp1oqdzmv37vPLYFGjuM/RmUgUMfD9vQfMszc54l55Y= +github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI= diff --git a/platform-engineer/go-dataframes/main.go b/platform-engineer/go-dataframes/main.go new file mode 100644 index 0000000..06df946 --- /dev/null +++ b/platform-engineer/go-dataframes/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "github.com/gocarina/gocsv" + "live_coding/models" + "os" +) + +func main() { + // Parse CSV into a grouped dataframe on the basis of the group ID + in, groupedDf := parseCSV() + defer closeSafely(in) + + // Map with + var analyzedGroups []models.AnalyzedGroup + + // Iterating over records to analyse each group + for groupId, _ := range groupedDf { + analyzedGroup := models.AnalyzedGroup{ + GroupID: groupId, + } + // TODO: Insert your code + // ... + analyzedGroups = append(analyzedGroups, analyzedGroup) + } + + // Write results to CSV + file, _ := os.OpenFile("analysis.csv", os.O_RDWR|os.O_CREATE, os.ModePerm) + // Close file connection + defer closeSafely(file) + //write the csv file + if err := gocsv.MarshalFile(&analyzedGroups, file); err != nil{ + panic(err) + } +} + +// Close connection to a file safely +func closeSafely(f *os.File) { + if err := f.Close(); err != nil { + panic(err) + } +} + +// Parse the CSV and group the data on the basis of the group ID +func parseCSV() (*os.File, map[int][]*models.DataFrame) { + in, err := os.Open("data.csv") + if err != nil { + panic(err) + } + + var dfs []*models.DataFrame + + if err := gocsv.UnmarshalFile(in, &dfs); err != nil { + panic(err) + } + + var groupedDf = make(map[int][]*models.DataFrame) + var exists bool + var df *models.DataFrame + + for i := range dfs { + df = dfs[i] + // Initializing the map slice + if _, exists = groupedDf[df.GroupID]; !exists { + groupedDf[df.GroupID] = []*models.DataFrame{} + } + + // Map group Ids to all the individual IDs + groupedDf[df.GroupID] = append(groupedDf[df.GroupID], df) + } + return in, groupedDf +} diff --git a/platform-engineer/go-dataframes/models/model.go b/platform-engineer/go-dataframes/models/model.go new file mode 100644 index 0000000..2fe90a7 --- /dev/null +++ b/platform-engineer/go-dataframes/models/model.go @@ -0,0 +1,16 @@ +package models + +// Frame of data in the CSV +type DataFrame struct { + GroupID int `csv:"group_id"` // .csv column headers + IndividualID int `csv:"individual_id"` + Attr1 string `csv:"attr_1"` + Attr2 string `csv:"attr_2"` +} + +// Analysis on a group +type AnalyzedGroup struct { + GroupID int `csv:"group_id"` +} + + diff --git a/platform-engineer/go-dataframes/pkg1/analysis.go b/platform-engineer/go-dataframes/pkg1/analysis.go new file mode 100644 index 0000000..5f95520 --- /dev/null +++ b/platform-engineer/go-dataframes/pkg1/analysis.go @@ -0,0 +1,7 @@ +package pkg1 + +import "live_coding/models" + +func count(df []*models.DataFrame) int { + return len(df) +} diff --git a/platform-engineer/go-dataframes/pkg2/analysis.go b/platform-engineer/go-dataframes/pkg2/analysis.go new file mode 100644 index 0000000..4f791ad --- /dev/null +++ b/platform-engineer/go-dataframes/pkg2/analysis.go @@ -0,0 +1 @@ +package pkg2