daru icon indicating copy to clipboard operation
daru copied to clipboard

Ability pivot table and keep the group_by columns in the DataFrame

Open info-rchitect opened this issue 8 years ago • 3 comments

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

info-rchitect avatar Jun 19 '17 10:06 info-rchitect

Makes sense.

zverok avatar Jun 20 '17 20:06 zverok

I will take a shot at it

info-rchitect avatar Jun 21 '17 13:06 info-rchitect

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

geekodour avatar Mar 08 '21 19:03 geekodour