# How to include a column with a constant value in a DataFrames select

**URL:** <https://discourse.julialang.org/t/how-to-include-a-column-with-a-constant-value-in-a-dataframes-select/98203>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [May 2, 2023, 2:14pm UTC](https://discourse.julialang.org/t/how-to-include-a-column-with-a-constant-value-in-a-dataframes-select/98203 "2023-05-02T14:14:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)\
**Post date:** [May 2, 2023, 2:14pm UTC](https://discourse.julialang.org/t/how-to-include-a-column-with-a-constant-value-in-a-dataframes-select/98203/1 "2023-05-02T14:14:26Z")

</div>

I have the following working example:

```julia
using DataFrames

# Create a DataFrame
data = DataFrame(Name=["Alice", "Bob", "Cathy", "David"],
                 Age=[30, 25, 27, 28],
                 City=["New York", "San Francisco", "Chicago", "Los Angeles"])

# Define a constant value
constant = 42

# Use the select function to include the constant value in each row of the DataFrame
new_data = select(data, :, :Name => ByRow(x -> constant) => :Constant)

```

Is there a better way of including a constant in this case, without resorting to passing a column, just for it to be ignored?

---

<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:** [May 2, 2023, 2:26pm UTC](https://discourse.julialang.org/t/how-to-include-a-column-with-a-constant-value-in-a-dataframes-select/98203/2 "2023-05-02T14:26:02Z")

</div>

In 1.8 and above there is a `Returns` struct which just returns whatever you tell it to, no matter what arguments you call it with

```julia
julia> select(df, [] => Returns(1) => :y)

```

---

<div class="post-metadata">

**Author:** ![kobusherbst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobusherbst/32/19682_2.png) [@kobusherbst](https://discourse.julialang.org/u/kobusherbst)\
**Post date:** [May 2, 2023, 2:56pm UTC](https://discourse.julialang.org/t/how-to-include-a-column-with-a-constant-value-in-a-dataframes-select/98203/3 "2023-05-02T14:56:37Z")

</div>

The updated example using @pdeffebach solution is:

```julia
using DataFrames

# Create a DataFrame
data = DataFrame(Name=["Alice", "Bob", "Cathy", "David"],
                 Age=[30, 25, 27, 28],
                 City=["New York", "San Francisco", "Chicago", "Los Angeles"])

# Define a constant value
constant_value = 42

# Use the select function to include the constant value in each row of the DataFrame
new_data = select(data, :, [] => Returns(constant_value) => :Constant)

```

---

<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:** [May 2, 2023, 2:58pm UTC](https://discourse.julialang.org/t/how-to-include-a-column-with-a-constant-value-in-a-dataframes-select/98203/4 "2023-05-02T14:58:13Z")

</div>

In DataFramesMeta.jl you can also just do

```julia
@transform df :Constant = 1

```
