Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP add: Dataframe based golang question #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions platform-engineer/go-dataframes/analysis.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
group_id
1
2
9 changes: 9 additions & 0 deletions platform-engineer/go-dataframes/data.csv
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions platform-engineer/go-dataframes/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module live_coding

go 1.16

require github.com/gocarina/gocsv v0.0.0-20210516172204-ca9e8a8ddea8
2 changes: 2 additions & 0 deletions platform-engineer/go-dataframes/go.sum
Original file line number Diff line number Diff line change
@@ -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=
72 changes: 72 additions & 0 deletions platform-engineer/go-dataframes/main.go
Original file line number Diff line number Diff line change
@@ -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
}
16 changes: 16 additions & 0 deletions platform-engineer/go-dataframes/models/model.go
Original file line number Diff line number Diff line change
@@ -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"`
}


7 changes: 7 additions & 0 deletions platform-engineer/go-dataframes/pkg1/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pkg1

import "live_coding/models"

func count(df []*models.DataFrame) int {
return len(df)
}
1 change: 1 addition & 0 deletions platform-engineer/go-dataframes/pkg2/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pkg2