-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
165 lines (134 loc) · 3.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"github.com/nitetrik/Godataframe/dataframe"
)
func main() {
// Create a new DataFrame
df, err := dataframe.NewDataFrame([]string{"Name", "Age", "City", "Salary"})
if err != nil {
fmt.Println("Error creating DataFrame:", err)
return
}
// Add columns to the DataFrame
names := []interface{}{"John", "Alice", "Bob", "Jane"}
df.AddColumn("Name", names)
ages := []interface{}{25, 30, 35, 28}
df.AddColumn("Age", ages)
cities := []interface{}{"New York", "London", "Tokyo", "Paris"}
df.AddColumn("City", cities)
salaries := []interface{}{50000, 60000, 75000, 55000}
df.AddColumn("Salary", salaries)
// Perform operations on the DataFrame
// Print header and data of the DataFrame
fmt.Println("DataFrame Header:")
df.PrintHeader()
fmt.Println()
fmt.Println("DataFrame Data:")
df.PrintData()
fmt.Println()
// Count the number of ages
count, err := df.Count("Age")
if err != nil {
fmt.Println("Error counting values:", err)
return
}
fmt.Println("Count of Ages:", count)
fmt.Println()
// Sum the salaries
sum, err := df.Sum("Salary")
if err != nil {
fmt.Println("Error summing values:", err)
return
}
fmt.Println("Sum of Salaries:", sum)
fmt.Println()
// Calculate the mean age
mean, err := df.Mean("Age")
if err != nil {
fmt.Println("Error calculating mean:", err)
return
}
fmt.Println("Mean Age:", mean)
fmt.Println()
// Filter the DataFrame
filteredDF, err := df.Filter(func(row int) bool {
return df.columns["Age"][row].(int) > 28
})
if err != nil {
fmt.Println("Error filtering DataFrame:", err)
return
}
fmt.Println("Filtered DataFrame:")
filteredDF.PrintHeader()
filteredDF.PrintData()
fmt.Println()
// Sort the DataFrame
err = df.Sort([]string{"Age", "Salary"}, true)
if err != nil {
fmt.Println("Error sorting DataFrame:", err)
return
}
fmt.Println("Sorted DataFrame:")
df.PrintHeader()
df.PrintData()
fmt.Println()
// Group and aggregate the DataFrame
groupedDF, err := df.GroupBy([]string{"City"})
if err != nil {
fmt.Println("Error grouping DataFrame:", err)
return
}
fmt.Println("Grouped DataFrame:")
groupedDF.PrintHeader()
groupedDF.PrintData()
fmt.Println()
// Join DataFrames
joinDF1, err := dataframe.NewDataFrame([]string{"Name", "Age", "City"})
if err != nil {
fmt.Println("Error creating join DataFrame 1:", err)
return
}
joinNames := []interface{}{"John", "Alice"}
joinDF1.AddColumn("Name", joinNames)
joinAges := []interface{}{25, 30}
joinDF1.AddColumn("Age", joinAges)
joinCities := []interface{}{"New York", "London"}
joinDF1.AddColumn("City", joinCities)
joinDF2, err := dataframe.NewDataFrame([]string{"City", "Population"})
if err != nil {
fmt.Println("Error creating join DataFrame 2:", err)
return
}
joinCities2 := []interface{}{"New York", "London"}
joinDF2.AddColumn("City", joinCities2)
joinPopulations := []interface{}{8500000, 9000000}
joinDF2.AddColumn("Population", joinPopulations)
joinedDF, err := dataframe.Join([]*dataframe.DataFrame{joinDF1, joinDF2}, []string{"City"})
if err != nil {
fmt.Println("Error joining DataFrames:", err)
return
}
fmt.Println("Joined DataFrame:")
joinedDF.PrintHeader()
joinedDF.PrintData()
fmt.Println()
// Serialize DataFrame to JSON
jsonData, err := df.SerializeToJSON()
if err != nil {
fmt.Println("Error serializing DataFrame to JSON:", err)
return
}
fmt.Println("Serialized DataFrame (JSON):")
fmt.Println(jsonData)
fmt.Println()
// Serialize DataFrame to CSV
csvData, err := df.SerializeToCSV()
if err != nil {
fmt.Println("Error serializing DataFrame to CSV:", err)
return
}
fmt.Println("Serialized DataFrame (CSV):")
fmt.Println(csvData)
fmt.Println()
}