# DataFrame: how to change value of a cell without knowing the row number

**URL:** <https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [March 19, 2019, 8:15am UTC](https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024 "2019-03-19T08:15:35Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)\
**Post date:** [May 12, 2022, 6:09am UTC](https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024/21 "2022-05-12T06:09:18Z")

</div>

Is there any plan for functionality like pandas index and loc ?  
Something like:

```julia
df = DataFrame(name=["John", "Sally", "Kirk"], age=[23., 42., 59.], children=[3,5,2])

setindex!( df, :name )

@chain df begin

    @transform :Sally.age = 30

    @rtransform :Age_relative_to_Sally = :age - :Sally.age

end

```

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [January 25, 2023, 7:46am UTC](https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024/22 "2023-01-25T07:46:52Z")

</div>

Hi,  
The solution proposed here does not seem to be compatible with last DataFrame version. What is the new syntax ? Thanks !

```julia
julia> using DataFrames, OhMyREPL

julia> df = DataFrame(name=["John", "Sally", "Kirk"], age=[23., 42., 59.], children=[3,5,2])
3×3 DataFrame
 Row │ name age children 
     │ String Float64 Int64    
─────┼───────────────────────────
   1 │ John 23.0 3
   2 │ Sally 42.0 5
   3 │ Kirk 59.0 2

julia> df[(df[:name] .== "Sally"),:age]=99
ERROR: ArgumentError: syntax df[column] is not supported use df[!, column] instead
Stacktrace:
 [1] getindex(#unused#::DataFrame, #unused#::Symbol)
   @ DataFrames ~/.julia/packages/DataFrames/dgZn3/src/abstractdataframe/abstractdataframe.jl:2595
 [2] top-level scope
   @ REPL[3]:1

julia> df[(df[!,:name] .== "Sally"),:age]=99
ERROR: MethodError: no method matching setindex!(::DataFrame, ::Int64, ::BitVector, ::Symbol)
Closest candidates are:
  setindex!(::DataFrame, ::Any, ::Integer, ::Union{AbstractString, Signed, Symbol, Unsigned}) at ~/.julia/packages/DataFrames/dgZn3/src/dataframe/dataframe.jl:687
  setindex!(::DataFrame, ::AbstractVector, ::AbstractVector, ::Union{AbstractString, Signed, Symbol, Unsigned}) at ~/.julia/packages/DataFrames/dgZn3/src/dataframe/dataframe.jl:716
  setindex!(::AbstractDataFrame, ::Any, ::CartesianIndex{2}) at ~/.julia/packages/DataFrames/dgZn3/src/other/broadcasting.jl:5
  ...
Stacktrace:
 [1] top-level scope

```

---

<div class="post-metadata">

**Author:** ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)\
**Post date:** [January 25, 2023, 8:25am UTC](https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024/23 "2023-01-25T08:25:01Z")

</div>

Notice `df[(df[:name] .== "Sally"),:age]=99` won’t work because `df[:name]` is not a syntax of DataFrames. You need to specify the rows, even if it’s all rows. So it should be `df[:, :name]` or `df[!,:name]`. If you want to avoid indicating “all rows”, just write `df.name`.

As for the second line you executed, `df[(df.name.== "Sally"),:age]` returns a vector. So you need to use Julia’s syntax to modify a vector. This means either:

- `df[(df.name.== "Sally"),:age] = [99]`
- `df[(df.name.== "Sally"),:age] .= 99`

---

<div class="post-metadata">

**Author:** ![Fred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred/32/14175_2.png) [@Fred](https://discourse.julialang.org/u/Fred)\
**Post date:** [January 25, 2023, 8:30am UTC](https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024/24 "2023-01-25T08:30:46Z")

</div>

@alfaromartino thank you very much, your solution works fine 🙂

```julia
julia> df[(df.name.== "Sally"),:age] = [99]
1-element Vector{Int64}:
 99

julia> df[(df.name.== "Sally"),:age] .= 99
1-element view(::Vector{Float64}, [2]) with eltype Float64:
 99.0

```

[Previous page](https://discourse.julialang.org/t/dataframe-how-to-change-value-of-a-cell-without-knowing-the-row-number/22024.md?page=1)
