# How to globally change a data frame inside for loop?

**URL:** https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361
**Category:** Data
**Tags:** question, data, dataframes, for-loop
**Created:** [May 25, 2023, 7:03am UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361 "2023-05-25T07:03:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yao\_Vang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yao_vang/32/49401_2.png) [@Yao\_Vang](https://discourse.julialang.org/u/Yao_Vang)
#### Post date: [May 25, 2023, 7:03am UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361/1 "2023-05-25T07:03:45Z")

</div>

Hi there!

How to apply changes in for loops to be kept outside of for loop too?  
I have a dataframe that is read inside a for loop via a function and and after bunch of computation, I apply a filter command over it and hope to have the changed dataframe for the next iterations of for loop. But the dataframe `df` does not change. How can I ask julia to keep changes and read the chnaged `df` at each iteration?

```julia
for i in I
      # read df
      # do some computation...
     elements= find_removeable_indices(x) # this function returns some indices that we want to remove from df later 
    # do some other computation...
    filter!(row -> (row.Index ∉ elements) && row.Price >= current_Price + shift , df) # df is the original dataframe with 1000 rows and 50 columns. One of the columns is `index`, the other is `Price` and shift is a constant
end

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [May 25, 2023, 7:31am UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361/2 "2023-05-25T07:31:11Z")

</div>

Maybe a clean way to do this would be to put the for loop in a function that gives the data frame as return value. But it would help if you could give a complete example: in your code example it’s not clear where the `df` variable is defined and where it’s used.

---

<div class="post-metadata">

### Author: ![Yao\_Vang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yao_vang/32/49401_2.png) [@Yao\_Vang](https://discourse.julialang.org/u/Yao_Vang)
#### Post date: [May 25, 2023, 7:41am UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361/3 "2023-05-25T07:41:01Z")

</div>

@sijo The `df` is defined outside of this for loop and it is read from a csv file.  
My understanding of `!` is that it should change anything regardless of its local domain. Is that right?  
And filter should return those rows from `df` that met the condition. In this case return those rows in `df` where their `index` columns are not in `elements` and also their `price` are greater than the current shifted price. Is this a correct interpretation?

Is there any alternative to `filter` to allow for removing some rows from a dataframe?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 25, 2023, 7:41am UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361/4 "2023-05-25T07:41:03Z")

</div>

Indeed an example would be good, in the REPL:

```julia
julia> df = DataFrame(x = rand(1_000));

julia> for i ∈ 1:10
           println(nrow(df))
           filter!(:x => >(i/10), df)
           println(nrow(df))
       end
1000
899
899
798
798
695
695
600
600
505
505
393
393
296
296
188
188
92
92
0

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 26, 2023, 6:06pm UTC](https://discourse.julialang.org/t/how-to-globally-change-a-data-frame-inside-for-loop/99361/5 "2023-05-26T18:06:16Z")

</div>

One probable problem derive from reading df internally the for loop, as seams you have done.
