# Dataframe to csv with the index column

**URL:** https://discourse.julialang.org/t/dataframe-to-csv-with-the-index-column/50400
**Category:** New to Julia
**Created:** [November 18, 2020, 9:11pm UTC](https://discourse.julialang.org/t/dataframe-to-csv-with-the-index-column/50400 "2020-11-18T21:11:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![acycliq](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@acycliq](https://discourse.julialang.org/u/acycliq)
#### Post date: [November 18, 2020, 9:11pm UTC](https://discourse.julialang.org/t/dataframe-to-csv-with-the-index-column/50400/1 "2020-11-18T21:11:51Z")

</div>

How to write a dataframe with the index column to a csv please? This is what I have put togeter but I am missing the index. (Btw, I dont even know if thats the best way. What I want, is to save a nested list of lists to a csv along with some identifier)

```julia
using CSV
using DataFrames

lis = [[1,2,3,4], [3], [5, 8]]
d = Dict( (i => [lis[i]] for i=1:length(lis)))
df = DataFrame(d)
df = DataFrame(Matrix(df)')
df = select(df, "x1" => "col_1")
CSV.write("df.csv", df, header=true)

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [November 18, 2020, 9:25pm UTC](https://discourse.julialang.org/t/dataframe-to-csv-with-the-index-column/50400/2 "2020-11-18T21:25:45Z")

</div>

A quick look at the [docs](https://csv.juliadata.org/stable/#CSV.write) and it seems like there isn’t a way to have the row number be printed. Just do

```julia
julia> insertcols!(df, 1, :Row => 1:nrow(df))
3×2 DataFrame
│ Row │ Row │ col_1 │
│ │ Int64 │ Adjoint… │
├─────┼───────┼───────────┤
│ 1 │ 1 │ [1 2 3 4] │
│ 2 │ 2 │ [3] │
│ 3 │ 3 │ [5 8] │

```

and it will save with a column `Row`.

But it sounds like you are having some trouble with DataFrames constructors. Maybe do this to create your data frame

```julia
julia> d = DataFrame(Row = 1:length(lis), Value = lis)
3×2 DataFrame
│ Row │ Row │ Value │
│ │ Int64 │ Array… │
├─────┼───────┼──────────────┤
│ 1 │ 1 │ [1, 2, 3, 4] │
│ 2 │ 2 │ [3] │
│ 3 │ 3 │ [5, 8] │

```

---

<div class="post-metadata">

### Author: ![acycliq](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@acycliq](https://discourse.julialang.org/u/acycliq)
#### Post date: [November 18, 2020, 9:33pm UTC](https://discourse.julialang.org/t/dataframe-to-csv-with-the-index-column/50400/3 "2020-11-18T21:33:48Z")

</div>

> [@pdeffebach](#):
>
> `insertcols!(df, 1, :Row => 1:nrow(df))`

Thanks so much"

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [November 18, 2020, 9:42pm UTC](https://discourse.julialang.org/t/dataframe-to-csv-with-the-index-column/50400/4 "2020-11-18T21:42:26Z")

</div>

Also note that you can transpose a data frame using `permutedims`. And calling a `DataFrame` constructor with a `Matrix` is deprecated. Use `DataFrame(Matrix(df)', :auto)`.
