Add method to parse string columns into list of string columns
See original GitHub issueOne of the most needed functions in data analysis is the operation among multiple columns and generation of new columns (and rows). The need can be abstracted to a unified method like List<Column> columnOperate(List<Column>)
to accomplish inter-column operation tasks. But now, I encountered an inter-column operation problem which cannot be solved efficiently and elegantly using only a few methods. In fact I found I couldn’t solve this basic need using methods given in tablesaw. Details is provided below.
Let’s say I have a Table named “df” with two columns “multi_ratio” and “amount”.
df
amount | multi_ratio |
--------------------------
100 | 0.8,0.2 |
200 | 0.5,0.5 |
Now I need to
- split every value in col “multi_ratio” into multiple values (e.g., convert “0.8,0.2” to List of 0.8, 0.2) ,
- amount * multi_ratio (split) (e.g., 100 * List of 0.8, 0.2 -> List of 80, 20) ,
- result of 2. expanded to multiple rows.
So the final result I need would be.
df2
amount | multi_ratio_single | multiply_result |
-----------------------------------------------------
100 | 0.8 | 80 |
100 | 0.2 | 20 |
200 | 0.5 | 100 |
200 | 0.5 | 100 |
To achieve this goal, I firstly make an empty copy of df
and add empty columns.
Table df2 = df.emptyCopy();
df2.addColumns(
StringColumn.create("multi_ratio_single"),
DoubleColumn.create("result")
);
Then, I tried to operate on each row of df
, to generate new rows and add them to df2
, but it seems just not work.
Does anybody have suggests to make this happen efficiently?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top GitHub Comments
Hi @eric-liuyd https://github.com/eric-liuyd,
I would look at the Table:melt() method, which implements the “tidy” melt operation, but I don’t have time to confirm and write it up.
OTOH, if I were doing it myself, I might do something like what @ccleva suggests: basically copying into a new table in a loop.
Hope one of these works.
larry
On Mon, Sep 13, 2021 at 6:13 AM ccleva @.***> wrote:
Hi ccleva. Thanks for your code. It really works. I didn’t get the use of append() before.