# Is there a way to read a DataFrame from file specifying the type of each column?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-read-a-dataframe-from-file-specifying-the-type-of-each-column/122146>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 1, 2024, 5:54pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-read-a-dataframe-from-file-specifying-the-type-of-each-column/122146 "2024-11-01T17:54:40Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [November 1, 2024, 6:36pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-read-a-dataframe-from-file-specifying-the-type-of-each-column/122146/7 "2024-11-01T18:36:00Z")

</div>

> [@world-peace](#):
>
> The problem with `CSV.read` is it requires a `filename` as the argument, whereas what I need is a `CSV.File`…

Why exactly do you want a `CSV.File`? If you want to end up with a `DataFrame` you can use `DataFrame` as `sink` in `CSV.read`.

> [@world-peace](#):
>
> is there a way to _convert_ the columns using something equivalent to this python code:

You could just manually convert after reading, using something like

```julia-repl
julia> df = CSV.read("test.csv", DataFrame) # initial DataFrame
2×3 DataFrame
 Row │ Col1 Col2 Col3
     │ Int64 Float64 String3
─────┼─────────────────────────
   1 │ 1 2.3 sdf
   2 │ 12 -0.213 ds

julia> for (col_symb, col_type) in zip((:Col1, :Col3), (Float32, String))
           df[!, col_symb] .= convert.(col_type, df[!, col_symb])
       end

julia> df
2×3 DataFrame
 Row │ Col1 Col2 Col3
     │ Float32 Float64 String
─────┼──────────────────────────
   1 │ 1.0 2.3 sdf
   2 │ 12.0 -0.213 ds

```

as in this topic

> [@DataFrames: convert column data type](https://discourse.julialang.org/t/dataframes-convert-column-data-type/35522/4):
>
> A more general way to do this is (assuming the column is called x) df[!,:x] = convert.(Bool,df[!,:x])

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-way-to-read-a-dataframe-from-file-specifying-the-type-of-each-column/122146)._
