# Pivoting results

**URL:** <https://discourse.julialang.org/t/pivoting-results/107914>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [December 21, 2023, 3:42pm UTC](https://discourse.julialang.org/t/pivoting-results/107914 "2023-12-21T15:42:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianhfield](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@brianhfield](https://discourse.julialang.org/u/brianhfield)\
**Post date:** [December 21, 2023, 3:42pm UTC](https://discourse.julialang.org/t/pivoting-results/107914/1 "2023-12-21T15:42:41Z")

</div>

```julia
df = DataFrame();
df[!,:Vintage] = ["2021-01-31","2021-01-31","2021-01-31","2021-02-28","2021-02-28",
    "2021-03-31","2021-04-30","2021-04-30"];
df[!,:Grade] = ["A","B","C","C","A","B","C","D"];
df[!,:Amt] = [2000000,999999,850000,450150,5075000,775000,625000,575000];
df

# Desired output:
desired = DataFrame();
desired[!,"Grade"] = ["A","B","C","D"];
desired[!,"01-31-21"] = [2000000,999999,850000,0];
desired[!,"02-28-21"] = [5075000,0,450150,0];
desired[!,"03-31-21"] = [0,775000,0,0];
desired[!,"04-30-21"] = [0,0,625000,575000];
desired

```

Can someone tell me why the below does not work?

```julia
desired = df |>
    groupby(:Grade) |>
    pivot_table(values = :Amt, cols = pd.to_datetime(:Vintage), aggfunc = sum, fill_value = 0) |>
    rename!(cols = x -> Dates.format(x, "m-dd-y"))

```

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [December 21, 2023, 3:50pm UTC](https://discourse.julialang.org/t/pivoting-results/107914/2 "2023-12-21T15:50:33Z")

</div>

Hi welcome to the Julia Discourse!

Please follow these instructions to make your code easier to read for us

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![gsinha](https://avatars.discourse-cdn.com/v4/letter/g/57b2e6/32.png) [@gsinha](https://discourse.julialang.org/u/gsinha)\
**Post date:** [December 21, 2023, 3:54pm UTC](https://discourse.julialang.org/t/pivoting-results/107914/3 "2023-12-21T15:54:50Z")

</div>

I think this is what you need:

```julia
df = DataFrame();
df[!,:Vintage] = [“2021-01-31”,“2021-01-31”,“2021-01-31”,“2021-02-28”,“2021-02-28”,
“2021-03-31”,“2021-04-30”,“2021-04-30”];
df[!,:Grade] = [“A”,“B”,“C”,“C”,“A”,“B”,“C”,“D”];
df[!,:Amt] = [2000000,999999,850000,450150,5075000,775000,625000,575000];
df

unstack(df, :Grade, :Vintage, :Amt, combine=mean, fill=0)

```

---

<div class="post-metadata">

**Author:** ![brianhfield](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@brianhfield](https://discourse.julialang.org/u/brianhfield)\
**Post date:** [December 21, 2023, 3:55pm UTC](https://discourse.julialang.org/t/pivoting-results/107914/4 "2023-12-21T15:55:57Z")

</div>

AWESOME! Thanks GSINHA! Only adjustment is the “mean” to “sum”.

Thank you!
