# How do I change the values of a dataframe within a dictionary

**URL:** <https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231>\
**Category:** Data\
**Tags:** question, dictionary, dataframes, help-database\
**Created:** [August 28, 2021, 3:58pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231 "2021-08-28T15:58:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 28, 2021, 3:58pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/1 "2021-08-28T15:58:30Z")

</div>

I’d like to know how to change the values of a dataframe, and ultimately a dataframe within a dictionary.

```julia
starting_balance=(
starting_balance["debit"]["assets"][account]=>(
		DataFrame( 
        date=Vector{String}("31 Dec 2019"),
		memo=Vector{String}("2019 closing balance"),
        debit=Vector{Dec64}(2000.56),
        credit=Vector{Dec64}(0),
		balance=Vector{Dec64}(2567.67)))

#Now I want to change this to

	starting_balance["debit"]["assets"][account]=>(
		DataFrame( 
        date=Vector{String}("1 Jan 2020"),
		memo=Vector{String}("2020 starting balance"),
        debit=Vector{Dec64}(0),
        credit=Vector{Dec64}(0),
		balance=Vector{Dec64}(	
				starting_balance[end,:].balance)))

```

I know this is broken, but it should illustrate what I’m trying to do.

---

<div class="post-metadata">

**Author:** ![DataFrames](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@DataFrames](https://discourse.julialang.org/u/DataFrames)\
**Post date:** [August 28, 2021, 11:11pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/2 "2021-08-28T23:11:22Z")

</div>

I guess if you change the structure of your DataFrame, you won’t need dictionary in the first place, currently your data are like

```julia
date | memo | debit | credit | balance
...

```

add three more columns

```julia
type1 | type2 | account | date | memo | debit | credit | balance
------|-------|...
debit | asset | acc |....

```

then you’ll have a long data frame and you can just manage it by DataFrames.jl APIs

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 29, 2021, 12:36am UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/3 "2021-08-29T00:36:26Z")

</div>

I was using dictionaries to create a hiarchy of accounts

```
             debit credit

```

drawings expenses assets liabilities equity revenue

actual accounts

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 29, 2021, 12:37am UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/4 "2021-08-29T00:37:35Z")

</div>

I guess you kinda see that, I’m worried I would have to rewrite a bunch of stuff. All I want to do is move the balances from one year to another.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [August 29, 2021, 8:35am UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/5 "2021-08-29T08:35:01Z")

</div>

Do you want to append a row to a data frame or to update the last row of the data frame?

From your question I see you want to update the row, so it should be just:

```julia
starting_balance["debit"]["assets"][account][end, [:date, :memo, :debit, :credit]] = ["1 Jan 2020", "2020 starting balance", 0, 0]

```

If you want to append a row do:

```julia
push!(starting_balance["debit"]["assets"][account],
      ["1 Jan 2020", "2020 starting balance", 0, 0, starting_balance["debit"]["assets"][account].balance[end]])

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 29, 2021, 2:50pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/6 "2021-08-29T14:50:54Z")

</div>

No I want to delete everything and have 1 row with the final balance.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 29, 2021, 3:14pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/7 "2021-08-29T15:14:58Z")

</div>

If you only want it to have the new row, it would be simplest to replace the dataframe in the dict with a new one. This assume you have no reference to the dataframe stored somewhere else.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 29, 2021, 3:27pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/8 "2021-08-29T15:27:21Z")

</div>

I did a rewrite, but can this be done with a for loop?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 29, 2021, 5:04pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/9 "2021-08-29T17:04:51Z")

</div>

If I understand what you want, I think all you need to do is

```julia
starting_balance["debit"]["assets"][account]=DataFrame( 
        date=Vector{String}("1 Jan 2020"),
		memo=Vector{String}("2020 starting balance"),
        debit=Vector{Dec64}(0),
        credit=Vector{Dec64}(0),
		balance=Vector{Dec64}(	
				starting_balance[end,:].balance))

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 29, 2021, 5:09pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/10 "2021-08-29T17:09:45Z")

</div>

There is no way that can work, since now I’m not telling it what DataFrame to use

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [August 29, 2021, 5:10pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/11 "2021-08-29T17:10:29Z")

</div>

I entered `starting_balance[end,:].balance` and it just gave me a method error, as I expected.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 29, 2021, 5:26pm UTC](https://discourse.julialang.org/t/how-do-i-change-the-values-of-a-dataframe-within-a-dictionary/67231/12 "2021-08-29T17:26:06Z")

</div>

Try this

```julia
starting_balance["debit"]["assets"][account]=DataFrame( 
        date=Vector{String}("1 Jan 2020"),
		memo=Vector{String}("2020 starting balance"),
        debit=Vector{Dec64}(0),
        credit=Vector{Dec64}(0),
		balance=Vector{Dec64}(balance))

```

But maybe that’s not what you want. It’s hard to tell.
