# Accessing data in a CSV row?

**URL:** https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057
**Category:** New to Julia
**Created:** [February 21, 2019, 8:20pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057 "2019-02-21T20:20:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [February 21, 2019, 8:20pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/1 "2019-02-21T20:20:02Z")

</div>

i’m at a loss. i’m using CSV just as row reader, and i find i can’t actually access any of the data in the row.

```julia
println(row[1])
ERROR: LoadError: MethodError: no method matching getindex(::CSV.Row{CSV.File{false,true,Parsers.BufferedIO{IOStream},Parsers.Delimited{false,true,Parsers.Quoted{Parsers.Strip{Parsers.Sentinel{typeof(Parsers.defaultparser),Parsers.Trie{0x00,false,missing,2,Tuple{}}}}},Parsers.Trie{0x00,false,missing,8,Tuple{Parsers.Trie{0x2c,true,missing,8,Tuple{}}}}},NamedTuple{(),Tuple{}}}}, ::Int64)

```

doesn’t work

```julia
for x in row
  println(x)
end
ERROR: LoadError: MethodError: no method matching iterate(::CSV.Row{CSV.File{false,true,Parsers.BufferedIO{IOStream},Parsers.Delimited{false,true,Parsers.Quoted{Parsers.Strip{Parsers.Sentinel{typeof(Parsers.defaultparser),Parsers.Trie{0x00,false,missing,2,Tuple{}}}}},Parsers.Trie{0x00,false,missing,8,Tuple{Parsers.Trie{0x2c,true,missing,8,Tuple{}}}}},NamedTuple{(),Tuple{}}}})

```

doesn’t work.

Then I realized I don’t actually know how to figure out what a row is and how i can access the elements of a row. I even went to github and looked at the code and i still can’t figure it out…

---

<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: [February 21, 2019, 8:46pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/3 "2019-02-21T20:46:00Z")

</div>

You access individual values via `getproperty`, so for example:

```julia
# read in csv file w/ two columns named "columnA" and "columnB"
file = CSV.File(filename)
for row in file
    println(row.columnA, row.columnB)
end

```

Oh, and you can see all the column names by doing `propertynames(row)`, so you could iterate through all the values of a row by doing:

```julia
for column in propertynames(row)
    value = getproperty(row, column)
end

```

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [February 21, 2019, 8:48pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/4 "2019-02-21T20:48:32Z")

</div>

yes. well. silly me didn’t assign column names. so i thought if i didn’t assign column names, then i would get back something that would simply respond to numeric indices.

so i added the column labels and solved that problem, although not before i tried row[“name”] and found that didn’t work either.

btw, is there anyway to force a missing value to be interpreted as “”, i.e. the empty string ?

thank you!

p.s. how do you handle missing value when trying to assign types ?  
if i know column1 is a float and column 2 might be missing OR a string, i would think i could do  
types=[Float64, Union{Missing,String}]  
but that doesn’t seem to work.

---

<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: [February 21, 2019, 8:55pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/5 "2019-02-21T20:55:42Z")

</div>

If your file doesn’t have column names, they’ll be automatically generated like `Column1,Column2,Column3` etc. You can also just pass them in yourself by doing `CSV.File(filename; header=["Column1", "Column2"], data=1)`.

For treating missing values as empty string, you could do:

```julia
using CSV, Tables
file = CSV.File(filename) |> Tables.transform(Column2=x->coalesce(x, ""))
for row in file
    # do stuff with row
end

```

What was the error when you tried `types=[Float64, Union{Missing, String}]`? I think that should work, but there might be a bug. In general, you can use the `allowmissing` keyword argument to control how things should be treated for an entire file: `allowmissing=:all` is the default and will make every column a `Union{T, Missing}` type; alternatively, you can do `allowmissing=:auto` and the type will only be `Union{T, Missing}` if null values are detected. The third option is `allowmissing=:none` which will error if any missing values are detected in the file.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [February 21, 2019, 9:04pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/6 "2019-02-21T21:04:38Z")

</div>

ERROR: LoadError: MethodError: Cannot `convert` an object of type Missing to an object of type String

I used allowmissing=:auto. The documentation doesn’t say which is default.

Thanks.

p.s. oh, good grief. while creating a MWE, i found that Union{Missing,String} DOES work. i have no idea why it’s crashing on my real file. I’m investigating.

---

<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: [February 21, 2019, 9:50pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/7 "2019-02-21T21:50:20Z")

</div>

Yeah, let me know if you can boil it down w/ a specific file. We can create an issue at the CSV.jl repo w/ the file or I can provide you w/ a way to share the file w/ me privately.

---

<div class="post-metadata">

### Author: ![purplishrock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/purplishrock/32/13451_2.png) [@purplishrock](https://discourse.julialang.org/u/purplishrock)
#### Post date: [February 21, 2019, 10:02pm UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/8 "2019-02-21T22:02:16Z")

</div>

very sneaky. buried in the file was another missing value in one of the _other_ string columns (i knew one of the columns had missing values, but there were just a scattering of missing values in another column and much farther down a very large file, so i didn’t seem them the first time i checked through the file).

problem solved, everything is as it should be 🙂

thanks for your help!

---

<div class="post-metadata">

### Author: ![benibilme](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@benibilme](https://discourse.julialang.org/u/benibilme)
#### Post date: [October 26, 2021, 12:33am UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/9 "2021-10-26T00:33:15Z")

</div>

How can access whole row in a single statement and convert to a single string if necessary?

---

<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: [October 26, 2021, 2:59am UTC](https://discourse.julialang.org/t/accessing-data-in-a-csv-row/21057/10 "2021-10-26T02:59:52Z")

</div>

Can you be more specific? I’m not quite sure what you’re asking for here.
