data-forge-ts icon indicating copy to clipboard operation
data-forge-ts copied to clipboard

Reshape table using Pivot

Open Grimi94 opened this issue 5 years ago • 5 comments

Hello I'm trying to reshape a table like the following example from: pydata Pandas Reshaping So far I have tried with the following code:

df = new dataForge.DataFrame({columns: {"foo": ["one", "one", "one", "two", "two","two"],
                                        "bar": ["a", "b", "c", "a", "b", "c"],
                                        "baz": [1, 2, 3, 4, 5, 6]}})
df = df.setIndex("foo").dropSeries("foo").bake()
df.pivot("bar", "baz", dataForge.Series.sum).toString()

which yields

__index__  bar  baz
---------  ---  ---
0          a    5  
1          b    7  
2          c    9  

But it seems to just ignore the index and group by the "baz" column. I have tried different variations but still I cant get it to work.

How can I achieve what is shown on the picture above?

Grimi94 avatar Jul 16 '20 20:07 Grimi94

Have you read the guide for the pivot function?

https://github.com/data-forge/data-forge-ts/blob/master/docs/guide.md#pivot

Try reading that first and you might find what you are looking for.

Otherwise I'll come back and have a look next week.

ashleydavis avatar Jul 16 '20 21:07 ashleydavis

Thanks for the suggestion, I have read the guide for the pivot function. What I'm trying to achieve is to group a column and then use that column as the header row while reshaping the dataframe. If we go back to the example on the image it is using the "foo" column as an index and then grouping and using the "bar" column as the new header on the output. The following is the result I'm trying to get:

__index__   a    b   c
---------  ---  --- ---
one         1    2   3  
two         4    5   6  

Grimi94 avatar Jul 18 '20 20:07 Grimi94

I'm sorry after reading the documentation for Pandas pivot and pivot_table I think that the Data-Forge pivot function works like pivot_table rather than pivot.

What do you ultimately hope to do with this? If you could explain the problem you are trying to solve I can probably find another way to help you achieve it.

ashleydavis avatar Jul 27 '20 01:07 ashleydavis

I am exploring the same problem today using data-forge. I think this chapter explains well the similar scenarios and rationales of pivot in R's tidyverse https://r4ds.had.co.nz/tidy-data.html

zyzhu avatar Jan 14 '21 03:01 zyzhu

Thanks for the info. I'm hoping to spend more time this year expanding Data-Forge, so stay tuned.

Although if you'd like to contribute to improving this code library, please do!

ashleydavis avatar Jan 15 '21 23:01 ashleydavis

Sorry, never got back to this issue.

If anyone else is able to contribute a PR for this I'll be open to reopen this issue.

ashleydavis avatar Apr 28 '23 23:04 ashleydavis