# How to get row indices and how can I change row indices/row names in a Julia DataFrame?

**URL:** <https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884>\
**Category:** Data\
**Tags:** dataframes\
**Created:** [April 23, 2021, 3:21pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884 "2021-04-23T15:21:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [April 23, 2021, 3:21pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/1 "2021-04-23T15:21:14Z")

</div>

If I take a Julia DataFrame as below

```julia
julia> using DataFrames
julia> df = DataFrame(col1 = ['α', 'β', 'γ'], col2 = ['A', 'B', 'C'])
3×2 DataFrame
│ Row │ col1 │ col2 │
│ │ Char │ Char │
├─────┼──────┼──────┤
│ 1 │ 'α' │ 'A' │
│ 2 │ 'β' │ 'B' │
│ 3 │ 'γ' │ 'C' │

```

We can get column names using` name()` function and change the column names using `rename!()` function also:

```julia
julia> names(df)
2-element Array{String,1}:
 "col1"
 "col2"

julia> rename!(df, ["I", "II"])
3×2 DataFrame
│ Row │ I │ II │
│ │ Char │ Char │
├─────┼──────┼──────┤
│ 1 │ 'α' │ 'A' │
│ 2 │ 'β' │ 'B' │
│ 3 │ 'γ' │ 'C' │

```

But how to get row names/indices from this DataFrame `df` and how to change the row indices?

---

<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:** [April 23, 2021, 3:30pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/2 "2021-04-23T15:30:30Z")

</div>

You cannot. DataFrames, unlike pandas, has no concept of row indices. You can obviously change the columns `"I"` and `"II"`, and you can _kind of_ treat those as indices via `findfirst`, but there isn’t really a one-to-one translation to pandas in that way.

---

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [April 23, 2021, 3:37pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/3 "2021-04-23T15:37:06Z")

</div>

Thank you @pdeffebach for this info. 🙂  
As of now we can notice DataFrames package is released with its 1.0 version. Will it never have any method to show/change rownames as we see in R/Pandas?

---

<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:** [April 23, 2021, 3:40pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/4 "2021-04-23T15:40:08Z")

</div>

Maybe! Not in the near future since it would be a fairly major addition to the API.

`GroupedDataFrame`s are also highly performant and allow indexing (via a `Tuple` or `NamedTuple`) of the grouping indices. That’s one way to emulate the behavior already.

---

<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:** [April 23, 2021, 4:13pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/5 "2021-04-23T16:13:49Z")

</div>

> Will it never have any method to show/change rownames as we see in R/Pandas?

In what use cases do you need this functionality? I am open to discuss adding it, but so far we were not given a use case that cannot be easily handled by what we provide.

---

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [April 23, 2021, 4:40pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/6 "2021-04-23T16:40:58Z")

</div>

I was looking at the TimeArray `ohlc` from `MarketData` package, which I can change into DataFrame:

```julia
julia> using MarketData, DataFrames

julia> ta = ohlc

julia> df = DataFrame(ta)
500×5 DataFrame
│ Row │ timestamp │ Open │ High │ Low │ Close │
│ │ Date │ Float64 │ Float64 │ Float64 │ Float64 │
├─────┼────────────┼─────────┼─────────┼─────────┼─────────┤
│ 1 │ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │
⋮
│ 499 │ 2001-12-28 │ 21.97 │ 23.0 │ 21.96 │ 22.43 │
│ 500 │ 2001-12-31 │ 22.51 │ 22.66 │ 21.83 │ 21.9 │

```

Now, I wanted to take that Date column as row-names/row-indices, as we manipulate time series indices in `Pandas`. I just became curious, how to get/change rownames in Julia DataFrame?

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [April 23, 2021, 4:46pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/7 "2021-04-23T16:46:47Z")

</div>

I used Pandas quite a bit, but found row indices rather annoying than useful - a row index is in the end just another column (with some special semantics in Pandas). Same for MultiIndex.  
I think it is good that they do not exist in DataFrames.jl.

---

<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:** [April 23, 2021, 5:06pm UTC](https://discourse.julialang.org/t/how-to-get-row-indices-and-how-can-i-change-row-indices-row-names-in-a-julia-dataframe/59884/8 "2021-04-23T17:06:21Z")

</div>

> I think it is good that they do not exist in DataFrames.jl.

I respect Pandas design as I know that a lot of thought was given to make it good (and it is super successful), but what you say is exactly what other Pandas users were telling me.

> Now, I wanted to take that Date column as row-names/row-indices

As @lungben commented - you can just keep it as a column and all will work. If you really want a fast lookup on this column then use `gdf = groupby(df, :timestamp)` and you can do `gdf[(your_date,)]` to get a subset of data frame matching this date fast.

Note that the fact that lookup is fast not guaranteed by Pandas (it is sometimes fast but not always - it depends on the data).
