1 Star 0 Fork 13

谋决量化/go_pandas

forked from 王布衣/pandas 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
dataframe_map.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
package pandas
import (
"fmt"
"sort"
)
// LoadMaps creates a new DataFrame based on the given maps. This function assumes
// that every map on the array represents a row of observations.
func LoadMaps(maps []map[string]interface{}, options ...LoadOption) DataFrame {
if len(maps) == 0 {
return DataFrame{Err: fmt.Errorf("load maps: empty array")}
}
inStrSlice := func(i string, s []string) bool {
for _, v := range s {
if v == i {
return true
}
}
return false
}
// Detect all colnames
var colnames []string
for _, v := range maps {
for k := range v {
if exists := inStrSlice(k, colnames); !exists {
colnames = append(colnames, k)
}
}
}
sort.Strings(colnames)
records := make([][]string, len(maps)+1)
records[0] = colnames
for k, m := range maps {
row := make([]string, len(colnames))
for i, colname := range colnames {
element := ""
val, ok := m[colname]
if ok {
element = fmt.Sprint(val)
}
row[i] = element
}
records[k+1] = row
}
return LoadRecords(records, options...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/moujue/go_pandas.git
git@gitee.com:moujue/go_pandas.git
moujue
go_pandas
go_pandas
master

搜索帮助