# How do you edit a DataFrame after reading it from a CSV?

**URL:** <https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235>\
**Category:** Data\
**Created:** [March 1, 2021, 6:19am UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235 "2021-03-01T06:19:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanjan\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjan_das/32/17886_2.png) [@Sanjan\_Das](https://discourse.julialang.org/u/Sanjan_Das)\
**Post date:** [March 1, 2021, 6:19am UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/1 "2021-03-01T06:19:16Z")

</div>

Hello! I’m having a bit of trouble with being able to edit a DataFrame after reading them in from a CSV. I’ve set it up as shown below.

```julia
julia> df = DataFrame(A = String[], B = Int[])
0×2 DataFrame

julia> CSV.write("test.csv", df)
"test.csv"

julia> df2 = DataFrame(CSV.File("test.csv"))
0×2 DataFrame

```

However, if I try to push! a row into this DataFrame, I get this.

```julia
julia> push!(df2, ("hello", 5))
┌ Error: Error adding value to column :A.
└ @ DataFrames C:\Users\xxxxx\.julia\packages\DataFrames\oQ5c7\src\dataframe\dataframe.jl:1532
ERROR: StackOverflowError:
Stacktrace:
 [1] push!(::SentinelArrays.MissingVector, ::String) at .\array.jl:982
 [2] append! at C:\Users\xxxxx\.julia\packages\SentinelArrays\Ubf17\src\missingvector.jl:109 [inlined]
 ... (the last 2 lines are repeated 52190 more times)
 [104383] push!(::SentinelArrays.MissingVector, ::String) at .\array.jl:982
 [104384] push!(::DataFrame, ::Tuple{String,Int64}; promote::Bool) at C:\Users\xxxxx\.julia\packages\DataFrames\oQ5c7\src\dataframe\dataframe.jl:1514
 [104385] push!(::DataFrame, ::Tuple{String,Int64}) at C:\Users\xxxxx\.julia\packages\DataFrames\oQ5c7\src\dataframe\dataframe.jl:1494

```

Is there an alternative to get around this error? I know that some usages of CSV tend to return an immutable DataFrame, but I’m unsure of how to make it mutable. I’m using DataFrames v0.22.5.

Any help appreciated!

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [March 1, 2021, 6:43am UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/2 "2021-03-01T06:43:33Z")

</div>

I get the error when the CSV is empty, but as long as there’s at least one datapoint, I’m able to add to the DataFrame without issues. This doesn’t really solve the problem but hopefully it’s a useful workaround.

---

<div class="post-metadata">

**Author:** ![Sanjan\_Das](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sanjan_das/32/17886_2.png) [@Sanjan\_Das](https://discourse.julialang.org/u/Sanjan_Das)\
**Post date:** [March 1, 2021, 7:37am UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/3 "2021-03-01T07:37:05Z")

</div>

This seems to do it, thanks! Not sure why it doesn’t work with no data either.

---

<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:** [March 1, 2021, 10:08am UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/4 "2021-03-01T10:08:41Z")

</div>

@Sanjan_Das , this [document](https://ahsmart.com/assets/pages/data-wrangling-with-data-frames-jl-cheat-sheet/DataFramesCheatSheet_v0.21_rev3.pdf) will give you a preview of the possibilities to edit dataframes.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [March 1, 2021, 10:30am UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/5 "2021-03-01T10:30:33Z")

</div>

> [@Sanjan\_Das](#):
>
> Not sure why it doesn’t work with no data either.

When there’s no data, CSV.jl has no way to determine the element type, so it sets it to `Missing`, so the column can only be pushed `missing` entries.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [March 1, 2021, 4:43pm UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/6 "2021-03-01T16:43:39Z")

</div>

Yeah, this is what’s really going on. Note you can provide column types manually, so you would end up with an empty String column instead of `Missing` which would allow modifictaion.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 1, 2021, 4:48pm UTC](https://discourse.julialang.org/t/how-do-you-edit-a-dataframe-after-reading-it-from-a-csv/56235/7 "2021-03-01T16:48:15Z")

</div>

I wonder if CSV should give a warning in this case.  
`"No rows, could not determine types, defaulting to missing. Solve this via passing ...."`  
Or even an error.  
It doesn’t seem like this is a super useful default behavour
