Giter Site home page Giter Site logo

Comments (7)

pjebs avatar pjebs commented on August 11, 2024 2

https://godoc.org/github.com/rocketlaunchr/dataframe-go#Apply
https://godoc.org/github.com/rocketlaunchr/dataframe-go#ApplyDataFrameFn

from dataframe-go.

pjebs avatar pjebs commented on August 11, 2024
var ctx = context.Background()

func main() {

	csvStr := `contact_number_country_code,contact_number
"973","12345678"`

	df, _ := imports.LoadFromCSV(ctx, strings.NewReader(csvStr), imports.CSVLoadOptions{
		DictateDataType: map[string]interface{}{
			"contact_number_country_code": "",
			"contact_number":              "",
		},
	})

	sConcatContactNumber := dataframe.NewSeriesString("concat_contact_number", &dataframe.SeriesInit{Size: df.NRows()})
	df.AddSeries(sConcatContactNumber, nil)

	applyFn := dataframe.ApplyDataFrameFn(func(vals map[interface{}]interface{}, row, nRows int) map[interface{}]interface{} {
		return map[interface{}]interface{}{
			"concat_contact_number": vals["contact_number_country_code"].(string) + vals["contact_number"].(string),
		}
	})

	_, err := dataframe.Apply(ctx, df, applyFn, dataframe.FilterOptions{InPlace: true})
	if err != nil {
		log.WithError(err).Error("concatenation cannot be applied")
	}

	fmt.Println(df)
}

from dataframe-go.

pjebs avatar pjebs commented on August 11, 2024

@migscabral

Can you try the sample I wrote above.

The issue is when you return the vals. The return map indicates what you want to change.

The key of map accepts ints (for index of Series) or strings (for name of series).

In your erroneous case:

 vals: map[0:973 1:12345678 2:<nil> concat_contact_number:97312345678 contact_number:12345678 contact_number_country_code:973]

It is saying change second column to nil but also saying change concat_contact_number Series to "97312345678". They point to the same Series in your case.

Either use ints or strings when referring to Series but not both.

from dataframe-go.

migscabral avatar migscabral commented on August 11, 2024

@pjebs I updated my code to return a new map from inside ApplyDataFrameFn and it now works. Thank you.

I have several questions so I may understand better how this library works:

  1. I'm not sure where in my code where the int or string keys were set. From what I understand I did not explicitly set to use the int or string keys. The vals map that was received by ApplyDataFrameFn already contained both. Can you point me where it was set?

  2. The key difference that I saw between your implementation and mine is that you created a new map inside the ApplyDataFrameFn instead of directly modifying the vals map. Is this the recommended way?

from dataframe-go.

pjebs avatar pjebs commented on August 11, 2024
  1. the vals param contains the existing values for the row with the key as an int (index) and string (name) for convenience.

  2. The applyFn must return a map that contains only what you want to update. You were basically rereturning the current row values (and not just the changes to update). I have updated the documentation to make it clearer.

from dataframe-go.

migscabral avatar migscabral commented on August 11, 2024

Thank you @pjebs much clearer now. Do you have a link to the said documentation?

from dataframe-go.

pjebs avatar pjebs commented on August 11, 2024

let me refresh godocs.org

from dataframe-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.