Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability pivot table and keep the group_by columns in the DataFrame #358

Open
info-rchitect opened this issue Jun 19, 2017 · 3 comments
Open

Comments

@info-rchitect
Copy link

Hi,

I noticed the pivot_table method removes group_by columns and uses them as the index in the newly created DF. Is there a way to do the pivot table and keep the group_by columns in the resulting DF? This would ensure any columns dependent on the group_by columns maintain their source of data.

regards

@zverok
Copy link
Collaborator

zverok commented Jun 20, 2017

Makes sense.

@info-rchitect
Copy link
Author

I will take a shot at it

@geekodour
Copy link

geekodour commented Mar 8, 2021

i come from the future to offer a solution to dem lost souls like me:

group_key = %i[a b c]
df = df.pivot_table(index: group_key, vectors: %i[x], agg: :sum, values: :value_col)
metric_vectors = df.vectors.to_a.map { |v| v.second.to_sym } # specific to my usecase
df.vectors = Daru::Index.new(metric_vectors)
df.index.name = group_key
df.reset_index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants