Using CSV.read() to import data from a data input file into a DataFrame

In my homemade function ‘readdata()’ I have:
Data = csv.read(filename; delim=’,’, DataFrame)
where ‘filename’ refers to one of my data inputfiles, named e.g. ‘instance.txt’.
This resulted from my code in julia-1.0.

When I try to run this, ‘Data’ remains empty; I cannot extract data from it.

You are getting keyword arguments and positional arguments mixed up

CSV.read(filename, DataFrame; delim = ',')
1 Like

When I do that I get the following:

Data = CSV.read(filename, DataFrame; delim=’,’)

#function read(source, sink=nothing; copycols::Bool=false, kwargs…)
#if sink === nothing
#Data = CSV.read(filename; delim=’,’, DataFrame)
#end
#Tables.CopiedColumns(CSV.File(source; kwargs…)) |> sink
#end

What to do with it?

You get 6 comments ?

First you should try this.

I got this comment:

‘’’
#function read(source, sink=nothing; copycols::Bool=false, kwargs…)
#if sink === nothing
#Data = CSV.read(filename; delim=’,’, DataFrame)
#end
#Tables.CopiedColumns(CSV.File(source; kwargs…)) |> sink
#end
‘’’

```
type the code here
```

#function read(source, sink=nothing; copycols::Bool=false, kwargs…)
..#if sink === nothing
....#Data = CSV.read(filename; delim=’,’, DataFrame)
..#end
..#Tables.CopiedColumns(CSV.File(source; kwargs…)) |> sink
#end

I copied the comment (with 6 lines). The indentation and my triple backtics disappeared.

you are not describing the problem you are encountering in your code

After my code line ‘Data = CSV.read(filename, DataFrame; delim=‘,’)’ Julia inserted 6 lines of code, all commented.
I didn’t know what to do with it, because I don’t understand what these 6 lines do.

When I uncomment them, and try to extract a collumn from Data by ‘x = Data[:1]’ Iget ‘UndefVarError: Data not defined’.

Run the following code:

df = DataFrame(a = [1, 2], b = [3, 4])
CSV.write("file.csv", df)
df_new = CSV.read("file.csv", DataFrame)

Julia is not inserting those comments anywhere. Some other program might be, an editor maybe?

Exactly how are you running the command?

1 Like

The commented code is broken, it looks like two code fragments randomly pasted together.

in addition, Data[:1] is not valid code either

what you should be getting is

julia> Data[:1]
ERROR: ArgumentError: syntax Data[column] is not supported use df[!, column] instead
...

Thank you! I’m nearly there, but not quite.
I have 5 collumns in my data file, and the first 4 went through.
So I changed naively [3, 4] into [3, 4, 5], but that didn’t work

Can you share the file? Or at least copy and paste the first few lines?

1511805.54010775,-3631401.61267572,134,14.0385102411102,1.58792636637882
1511880.54010775,-3631401.61267572,135,14.0623467964249,1.63997535275912
1511955.54010775,-3631401.61267572,136,13.8401810265915,1.48364309144577
1511730.54010775,-3631476.61267572,248,14.3080113191324,1.8129535268525
1511805.54010775,-3631476.61267572,249,14.2710283891669,1.78019664985144
1511880.54010775,-3631476.61267572,250,14.2895778872987,1.79291534883922
1511955.54010775,-3631476.61267572,251,13.9846463218381,1.55426381397442
1512030.54010775,-3631476.61267572,252,13.1978960888864,1.12702376308933
1512105.54010775,-3631476.61267572,253,12.0766192975667,0.867366055025797

Works perfectly for me.

Could you also extract the last column?

I run this via VSCode