Skip to content

replace function - replace different column than input column #72

Closed
@HA6Bots

Description

@HA6Bots

Hi,

I want to replace values in one column according to values in another.
So for example I have the dataframe:

index: {0, 1, 2, 3, 4, 5, 6, 7}
int_col: { 1, 2, 3, 50, 6, 7, 8, 30}
int_col_2: { 5,5,5,5,5,5,5,5}

And I want to replace int_col_2 values with 0 or 1, if the corresponding value at int_col is odd or even

So my desired output would be:

index: {0, 1, 2, 3, 4, 5, 6, 7}
int_col: { 1, 2, 3, 50, 6, 7, 8, 30}
int_col_2: { 0, 1, 0, 1, 1, 0, 1, 1}

Is this possible to achieve using the replace function using functors? I ask because it seems as though you can only pass one column for input which is the same one affected by the functor e.g.

struct ReplaceFunctor {

	bool operator() (const unsigned int& idx, int& value) {

		value *= 5;

		return (true);
	}

	size_t  count{ 0 };
};

ReplaceFunctor  functor;
dataframe.replace<int, ReplaceFunctor>("int_col", functor);

However to perform my task my ReplaceFunctor will have to look something like this:

struct ReplaceFunctor {

	bool operator() (const unsigned int& idx, int& value1, int& value2) {

		if(value1 % 2 == 0)
                    value2 = 1;
                else
                    value2 = 0;

		return (true);
	}

	size_t  count{ 0 };
};

Please let me know if there's a way to achieve this using the replace function (or any other function for that matter), or if there's an example of something like this that I missed in the docs.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions