Create a dataframe with two titles


#1

Julia DataFrames.jl seems like a really great way to manipulate data in a tabular form. https://juliadata.github.io/DataFrames.jl/stable/man/getting_started.html I was just wondering, is there a simple way to create a data frame with more than one titles? Or put some signature in the corner where the phrase “row” is sitting (see the following)?

julia> using DataFrames

julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrame
│ Row │ A     │ B      │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ M      │
│ 2   │ 2     │ F      │
│ 3   │ 3     │ F      │
│ 4   │ 4     │ M      │

EDIT: yes. I should have made this clear. The following might be a good context.
Suppose we have a process which starts at a certain time t. Then, this time t is a parameter / signature which only belongs to this process. And in the columns of this data frame, we have other parameters / characteristics. If it’s a table, then the starting time t can be manually put in the title part. For data frames, maybe it can not be done? Any comments are greatly appreciated.


#2

Can you give an example of what you want the output to look like? WHat is the “title” of a DataFrame?


#3

It is not clear what you are referring to — DataFrames have no “title”.

The Row in the table comes from the Base.show method, and is hardcoded. Making it customizable per dataframe would need a minor redesign of the library.

It is unclear what you need this for — perhaps you could provide some context.


#4

@Tamas_Papp, I have edited the original post.


#5

It looks like you want metadata:

TL;DR: AFAIK there are several experiments, but no solution one can call standard at the moment.


#6

With ReusePatterns.jl you can build a wrapper around a DataFrame object:

using DataFrames, ReusePatterns

mutable struct CustomDataFrame <: AbstractDataFrame
    p::DataFrame
    customtitle::String
    CustomDataFrame(args...; kw...) = new(DataFrame(args...; kw...), "")
    CustomDataFrame(df::DataFrame) = new(df, "")
end
@forward((CustomDataFrame, :p), DataFrame) # <-- reuse all existing methods

# Custom methods:
settitle!(df::CustomDataFrame, s::AbstractString) = setfield!(df, :customtitle, s)
Base.show(df::CustomDataFrame; kw...) = show(stdout, df; kw...)
function Base.show(io::IO, df::CustomDataFrame; kw...)
    println(io, "TITLE: ", getfield(df, :customtitle))
    show(io, getfield(df, :p); kw...)
end

and use it as follows:

df = CustomDataFrame(A = 1:4, B = ["M", "F", "F", "M"])
settitle!(df, "My custom title!")
show(df)

The output is:

julia> show(df)
TITLE: My custom title!
4×2 DataFrame
│ Row │ A     │ B      │
│     │ Int64 │ String │
├─────┼───────┼────────┤
│ 1   │ 1     │ M      │
│ 2   │ 2     │ F      │
│ 3   │ 3     │ F      │
│ 4   │ 4     │ M      │