pandas

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2023 License: MulanPSL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	XLSX = "xlsx"
	XLS  = "xls"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DataFrame

type DataFrame struct {
	dataframe.DataFrame
	// contains filtered or unexported fields
}

DataFrame is https://github.com/go-gota/gota/dataframe simple extend

func Read

func Read(path string, sheet ...string) DataFrame

Read 读取excel文档,返回 DataFrame

path: 文档路径 sheet: 读取的表格,默认Sheet1
Example
df := Read("test.xls")
if df.Error() != nil {
	panic(df.Error())
}
fmt.Println(df)

df1 := Read("test.xlsx")
if df1.Error() != nil {
	panic(df1.Error())
}
fmt.Println(df1)
Output:

func (*DataFrame) DropCols

func (d *DataFrame) DropCols(col ...string)

DropCols 批量删除

func (*DataFrame) FormatCols

func (d *DataFrame) FormatCols(f func(elem any) any, colName ...string)

FormatCols 批量格式化列

f(elem any) any:处理函数,elem 为列的每个数值,注意判断数据类型
colName:适用函f数整理的列名
Example
df := DataFrame{}
df.DataFrame = dataframe.LoadRecords(
	[][]string{
		{"A", "B", "C", "D"},
		{"a", "4", "5.1", "true"},
		{"k", "5", "7.0", "true"},
		{"k", "4", "6.0", "true"},
		{"a", "2", "7.1", "false"},
	},
)

df.FormatCols(func(elem any) any {
	switch v := elem.(type) {
	case float64:
		return v + 1
	case int:
		return v - 1
	default:
		return 0
	}
},
	"B", "C",
)
fmt.Println(df)
Output:

func (*DataFrame) MapCols

func (d *DataFrame) MapCols(cols ...string) [][]any

MapCols 将 DataFrame 转化为 列切片

func (*DataFrame) RenameCols

func (d *DataFrame) RenameCols(col map[string]string)

RenameCols 批量命名

func (*DataFrame) Row

func (d *DataFrame) Row(row int) series.Series

Row 返回一行的数据帧,数据类型字符串

func (*DataFrame) SetType

func (d *DataFrame) SetType(t map[string]series.Type)

SetType 设置列的数据类型,如果数据转换失败默认设置为文本类型

Example
df := DataFrame{}
df.DataFrame = dataframe.LoadRecords(
	[][]string{
		{"A", "B", "C", "D"},
		{"a", "4", "5.1", "true"},
		{"k", "5", "7.0", "true"},
		{"k", "4", "6.0", "true"},
		{"a", "2", "7.1", "false"},
	},
)
fmt.Println(df)

df.SetType(map[string]series.Type{"D": series.Int, "C": series.String})

fmt.Println(df)
Output:

func (*DataFrame) WriteXLSX

func (d *DataFrame) WriteXLSX(path string) error

WriteXLSX 将 DataFrame 写入 XLSX 文档中

Example
df := DataFrame{}
df.DataFrame = dataframe.LoadRecords(
	[][]string{
		{"A", "B", "C", "D"},
		{"a", "4", "5.1", "true"},
		{"k", "5", "7.0", "true"},
		{"k", "4", "6.0", "true"},
		{"a", "2", "7.1", "false"},
	},
)

err := df.WriteXLSX("test.xlsx")
if err != nil {
	panic(err)
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL