# Indexing a DataFrame Index

**URL:** <https://discourse.julialang.org/t/indexing-a-dataframe-index/92030>\
**Category:** Data\
**Tags:** question, dataframes\
**Created:** [December 23, 2022, 2:03am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030 "2022-12-23T02:03:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![wiseac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wiseac/32/36768_2.png) [@wiseac](https://discourse.julialang.org/u/wiseac)\
**Post date:** [December 23, 2022, 2:03am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/1 "2022-12-23T02:03:42Z")

</div>

Hello, I am trying to write a function that takes in a DataFrame that has an index, df[x:x2,c], and uses those index numbers for calculations.

An example would taking df[1:100,1] and then multiplying 1 and 100 by 2.

Whenever I try to create a function with df[x:x2,c] as a variable it throws this error, “df[x:x2, c]” is not a valid function argument name."

I was wondering if my way of naming the variable is wrong, or I just have to write a function with a variable that encompasses the whole index and then index the index within the function (which I cant figure out either).

Thanks,  
W

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 23, 2022, 2:40am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/2 "2022-12-23T02:40:50Z")

</div>

> [@wiseac](#):
>
> Whenever I try to create a function with df[x:x2,c] as a variable it throws this error, “df[x:x2, c]” is not a valid function argument name."

it would be helpful if you can share code snippet

---

<div class="post-metadata">

**Author:** ![wiseac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wiseac/32/36768_2.png) [@wiseac](https://discourse.julialang.org/u/wiseac)\
**Post date:** [December 23, 2022, 6:12am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/3 "2022-12-23T06:12:20Z")

</div>

```julia
(Cary1[1327:1528,2], Cary1[1634:1883,2], Cary1[2495:2680,2], Cary1[3211:3298,2], Cary1[3330:3505,2], Cary1[3558:3668,2], Cary1[3888:4078,2])

```

```julia
function ConvertCaryDftoCRDDf(CaryDF, CrdDF, Df[x:x2,c]...)
	DistanceDiff = length(CaryDF[!,1])/length(CrdDF[!,1])
	Replace = replace("$(Df)[$(round(x/DistanceDiff)):$(round(x2/DistanceDiff)),c]", "Df" => "CrdDF")
	
	ConvertedDF = eval(Meta.parse(Replace))
	return ConvertedDF
end

```

Basically I am trying to divide the indexes of one DataFrame and then set another DataFrame with those new indexes.

The DataFrames are on a different timescale so I am trying to match them.

---

<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:** [December 23, 2022, 6:48am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/4 "2022-12-23T06:48:47Z")

</div>

You have three options :

- pass them as a separate argument to your function
- store them as a column of your data frame
- store them as metadata of your data frame

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 23, 2022, 10:01am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/5 "2022-12-23T10:01:48Z")

</div>

> [@bkamins](#):
>
> store them as metadata of your data frame

How does this work?

---

<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:** [December 23, 2022, 10:12am UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/6 "2022-12-23T10:12:33Z")

</div>

```julia
julia> using DataFrames

julia> df = DataFrame(a=1:10, b=11:20)
10×2 DataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 1 11
   2 │ 2 12
   3 │ 3 13
   4 │ 4 14
   5 │ 5 15
   6 │ 6 16
   7 │ 7 17
   8 │ 8 18
   9 │ 9 19
  10 │ 10 20

julia> metadata!(df, "idxs", [2, 4, 6])
10×2 DataFrame
 Row │ a b
     │ Int64 Int64
─────┼──────────────
   1 │ 1 11
   2 │ 2 12
   3 │ 3 13
   4 │ 4 14
   5 │ 5 15
   6 │ 6 16
   7 │ 7 17
   8 │ 8 18
   9 │ 9 19
  10 │ 10 20

```

and now you can retrieve this metadata later:

```julia
julia> metadata(df, "idxs")
3-element Vector{Int64}:
 2
 4
 6

```

(here I made the metadata volatile, as I assume when mutating the data frame the correctness of the metadata can be lost)

---

<div class="post-metadata">

**Author:** ![wiseac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wiseac/32/36768_2.png) [@wiseac](https://discourse.julialang.org/u/wiseac)\
**Post date:** [December 23, 2022, 1:44pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/7 "2022-12-23T13:44:07Z")

</div>

Hello, thank you for explaining the metadata example.

Out of curiosity how would you accomplish “pass them as a separate argument to your function”

Best,  
W

---

<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:** [December 23, 2022, 1:45pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/8 "2022-12-23T13:45:56Z")

</div>

> [@wiseac](#):
>
> “pass them as a separate argument to your function”

E.g. define a signature as:

```julia
function ConvertCaryDftoCRDDf(CaryDF, CrdDF, Df, x:x2, c)

```

and inside you do not need to use metaprogramming as you have access to both `x:x2` and `c`.

---

<div class="post-metadata">

**Author:** ![wiseac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wiseac/32/36768_2.png) [@wiseac](https://discourse.julialang.org/u/wiseac)\
**Post date:** [December 23, 2022, 3:41pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/9 "2022-12-23T15:41:26Z")

</div>

I get this error.

```julia
"x:x2" is not a valid function argument name

```

---

<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:** [December 23, 2022, 3:47pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/10 "2022-12-23T15:47:32Z")

</div>

Ah - sorry - I did not make a correct argument names - I used names you used to show you where the arguments should go. The signature should be:

```julia
function ConvertCaryDftoCRDDf(CaryDF, CrdDF, Df, row_index, c)

```

and then pass `x:x2` as `row_index` positional argument.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 23, 2022, 3:54pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/11 "2022-12-23T15:54:22Z")

</div>

I would recommend you read the Julia docs or some beginner level tutorials to get a sense for how Julia works - your issues mainly seem to stem from a lack of understanding of base Julia syntax.

---

<div class="post-metadata">

**Author:** ![wiseac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wiseac/32/36768_2.png) [@wiseac](https://discourse.julialang.org/u/wiseac)\
**Post date:** [December 23, 2022, 4:04pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/12 "2022-12-23T16:04:43Z")

</div>

```julia
I would recommend you read the Julia docs or some beginner level tutorials to get a sense for how Julia works - your issues mainly seem to stem from a lack of understanding of base Julia syntax.

```

My lack of understanding is exactly why I am looking for help. I was unsure if there was a way to pass a DataFrame with and index as an argument. I tried looking for documentation online on the subject but I came up short.

---

<div class="post-metadata">

**Author:** ![wiseac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wiseac/32/36768_2.png) [@wiseac](https://discourse.julialang.org/u/wiseac)\
**Post date:** [December 23, 2022, 4:08pm UTC](https://discourse.julialang.org/t/indexing-a-dataframe-index/92030/13 "2022-12-23T16:08:23Z")

</div>

Thank you for your help. I will try both the metadata and the separate argument function to see which works best.
