# Copy metadata between DataFrames

**URL:** <https://discourse.julialang.org/t/copy-metadata-between-dataframes/107293>\
**Category:** Machine Learning\
**Tags:** mljlinearmodels\
**Created:** [December 7, 2023, 5:05pm UTC](https://discourse.julialang.org/t/copy-metadata-between-dataframes/107293 "2023-12-07T17:05:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [December 7, 2023, 5:05pm UTC](https://discourse.julialang.org/t/copy-metadata-between-dataframes/107293/1 "2023-12-07T17:05:12Z")

</div>

Is there a method to copy all of the metadata from one DataFrame to another?

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [September 27, 2024, 7:06pm UTC](https://discourse.julialang.org/t/copy-metadata-between-dataframes/107293/2 "2024-09-27T19:06:46Z")

</div>

for anyone that comes next:

```julia
md = DataFrames.metadata(df1)
for k in keys(md)
    DataFrames.metadata!(df2, k, md[k])
end

```

---

<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:** [September 30, 2024, 7:32pm UTC](https://discourse.julialang.org/t/copy-metadata-between-dataframes/107293/4 "2024-09-30T19:32:45Z")

</div>

Just in case you weren’t aware, there are two “styles” of metadata in Julia.

There is the `:note` style and the `:default` style.

In the `:default` style, metadata is dropped after every transformations. So something like

```julia
julia> df = DataFrame(a = 1);

julia> colmetadata!(df, :a, "label", "Label for a")
1×1 DataFrame
 Row │ a     
     │ Int64 
─────┼───────
   1 │ 1

julia> colmetadata(df)
Dict{Symbol, Dict{String, String}} with 1 entry:
  :a => Dict("label"=>"Label for a")

julia> df2 = transform(df, :a => ByRow(t -> t + 1) => :b);

julia> colmetadata(df2)
Dict{Any, Any}()

```

You might be struggling with this. The solution is to set the `colmetadatastyle` to `:note`

```julia
julia> df = DataFrame(a = 1);

julia> colmetadata!(df, :a, "label", "Label for a")
1×1 DataFrame
 Row │ a     
     │ Int64 
─────┼───────
   1 │ 1

julia> setcolmetadatastyle!(df; style =:note);

julia> df2 = transform(df, :a => ByRow(t -> t + 1) => :b);

julia> colmetadata(df)
Dict{Symbol, Dict{String, String}} with 1 entry:
  :a => Dict("label"=>"Label for a")

```

Two solutions

1. TableMetaDataTools.jl adds some convenience functions
2. DataFramesMeta.jl adds even more convenience functions. All metadata created with DataFramesMeta.jl is with the `:note` style. So they are forwarded.
