英文:
How to find difference between two column [col1-col2]
问题
我正在使用Go的gota/dataframe库。
我想要将两列的值相减,并将结果存储到一个新的列中。
我已经进行了很多搜索,但没有找到解决方法,请帮忙。
df := dataframe.ReadCSV(csvfile, dataframe.WithDelimiter(';'))
fmt.println(df)
输出结果:
accountId deposit Withdrawals
anil0001 50 10
vikas0002 10 10
ravi0003 20 10
user1111 NaN 20
我想要在一个新的列中计算deposit和Withdrawals的差值。
accountId deposit Withdrawals deposit_Withdrawals_diff
anil0001 50 10 40
vikas0002 10 10 0
ravi0003 20 10. 10
user1111 0 20. -20
你可以参考这里的gota库来实现。
英文:
I am using Go gota/dataframe.
I want to subtract value of 2 columns and store it into a new column.
I searched a lot but no luck, please help.
df := dataframe.ReadCSV(csvfile, dataframe.WithDelimiter(';'))
fmt.println(df)
Output:
accountId deposit Withdrawals
anil0001 50 10
vikas0002 10 10
ravi0003 20 10
user1111 NaN 20
I want diff of deposit and Withdrawals in new column
accountId deposit Withdrawals deposit_Withdrawals_diff
anil0001 50 10 40
vikas0002 10 10 0
ravi0003 20 10. 10
user1111 0 20. -20
答案1
得分: 1
Gota/dataframe看起来很有趣。所以我看了一下。
我对你的问题有一个基本可行的解决方案,但感觉不太好。也许有人能找到一个更简洁的解决方案。
package main
import (
"fmt"
"strings"
"github.com/go-gota/gota/dataframe"
"github.com/go-gota/gota/series"
)
func main() {
csvStr := `accountId,deposit,Withdrawals
anil0001,50,10
vikas0002,10,10
ravi0003,20,10
user1111,NaN,20`
df := dataframe.ReadCSV(strings.NewReader(csvStr))
// 在行内,元素通过它们的列索引进行索引。
indexDeposit := 1
indexWithdrawals := 2
// Rapply按行读取数据。
// 你可以使用s.Elem(index)或s.Val(index)访问行的每个元素。
// 要按列浏览,请使用Capply。
s := df.Rapply(func(s series.Series) series.Series {
deposit, err := s.Elem(indexDeposit).Int()
if err != nil {
return series.Ints("NAN")
}
withdrawal, err := s.Elem(indexWithdrawals).Int()
if err != nil {
return series.Ints("NAN")
}
return series.Ints(deposit - withdrawal)
})
// 通过调用Mutate将新的series追加到数据源中。
// 你可以打印s来查看其内容。
df = df.Mutate(s.Col("X0")).
Rename("deposit_Withdrawals_diff", "X0")
fmt.Println(df)
}
输出:
$ go run .
[4x4] DataFrame
accountId deposit Withdrawals deposit_Withdrawals_diff
0: anil0001 50 10 40
1: vikas0002 10 10 0
2: ravi0003 20 10 10
3: user1111 NaN 20 NaN
<string> <int> <int> <int>
https://play.golang.org/p/FPIxep_CW9P
英文:
Gota/dataframe look's interesting. So I had a look.
I have a somehow working solution for your question but it does not feel good. Maybe someone will find a cleaner solution.
package main
import (
"fmt"
"strings"
"github.com/go-gota/gota/dataframe"
"github.com/go-gota/gota/series"
)
func main() {
csvStr := `accountId,deposit,Withdrawals
anil0001,50,10
vikas0002,10,10
ravi0003,20,10
user1111,NaN,20`
df := dataframe.ReadCSV(strings.NewReader(csvStr))
// Within a row, elements are indexed by their column index.
indexDeposit := 1
indexWithdrawals := 2
// Rapply reads the data by rows.
// You can access each element of the row using
// s.Elem(index) or s.Val(index).
// To browse by columns use Capply.
s := df.Rapply(func(s series.Series) series.Series {
deposit, err := s.Elem(indexDeposit).Int()
if err != nil {
return series.Ints("NAN")
}
withdrawal, err := s.Elem(indexWithdrawals).Int()
if err != nil {
return series.Ints("NAN")
}
return series.Ints(deposit - withdrawal)
})
// The new series is appended to
// the data source via a call to Mutate.
// You can print s to read its content.
df = df.Mutate(s.Col("X0")).
Rename("deposit_Withdrawals_diff", "X0")
fmt.Println(df)
}
Outputs:
$ go run .
[4x4] DataFrame
accountId deposit Withdrawals deposit_Withdrawals_diff
0: anil0001 50 10 40
1: vikas0002 10 10 0
2: ravi0003 20 10 10
3: user1111 NaN 20 NaN
<string> <int> <int> <int>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论