如何找到两列之间的差异 [col1-col2]

huangapple go评论88阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;strings&quot;

	&quot;github.com/go-gota/gota/dataframe&quot;
	&quot;github.com/go-gota/gota/series&quot;
)

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(&quot;NAN&quot;)
		}
		withdrawal, err := s.Elem(indexWithdrawals).Int()
		if err != nil {
			return series.Ints(&quot;NAN&quot;)
		}
		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(&quot;X0&quot;)).
		Rename(&quot;deposit_Withdrawals_diff&quot;, &quot;X0&quot;)

	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
&lt;string&gt;  &lt;int&gt;   &lt;int&gt;       &lt;int&gt;

https://play.golang.org/p/FPIxep_CW9P

huangapple
  • 本文由 发表于 2021年9月16日 00:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/69197017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定