Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion - Matrix Syntax for hcat (as well as vcat) #3429

Open
Lincoln-Hannah opened this issue Mar 8, 2024 · 4 comments
Open

Suggestion - Matrix Syntax for hcat (as well as vcat) #3429

Lincoln-Hannah opened this issue Mar 8, 2024 · 4 comments
Labels
Milestone

Comments

@Lincoln-Hannah
Copy link

Lincoln-Hannah commented Mar 8, 2024

function hvcat(rows::Tuple{Vararg{Int}}, d1::DataFrame, n1::NamedTuple, xs... ) 
    
    Rows = reshape([d1,n1,xs...], 2, Int64(length(xs)/2)+1) |> permutedims  |> eachrow

    vcat([hcat(d, DataFrame( 
        
                n[1] isa AbstractArray ? n : fieldnames(n)[1] => repeat([n[1]],nrow(d)) 
           ))

        for (d,n) in Rows ]...)

end
  

########    Examples     #################
[
    DataFrame(a=1:2,b=1:2) (;c="a")
    DataFrame(a=3:4,b=3:4) (;c="b")
] 

[
    DataFrame(a=1:2) (;b=1:2,c=11:12)
    DataFrame(a=3:4) (;b=3:4,c=13:14)
    DataFrame(a=5:7) (;b=5:7,c=15:17)
] 

[
    DataFrame(a=1:2,b=1:2)   DataFrame(c=1:2,d=1:2)
]
@bkamins
Copy link
Member

bkamins commented Mar 8, 2024

Yes - we can consider adding hvcat in the next release.

@bkamins bkamins added this to the 1.7 milestone Mar 8, 2024
@Lincoln-Hannah
Copy link
Author

Lincoln-Hannah commented Apr 4, 2024

I often vcat a vector of DataFrames.
I don't think there is an easy way to add a column to show which vector element each dataframe came from.
This would be really useful. Something like:

vcat( [
    DataFrame(a=1:2,b=1:2) 
    DataFrame(a=3:4,b=3:4) 
]...,   
    extracolumn = :c => [  "df1",   "df2"  ]
)

output

:a   :b   :c
1    1    "df1"
2    2    "df1"
3    2    "df2"
4    2    "df2"

@bkamins
Copy link
Member

bkamins commented Apr 4, 2024

julia> vcat(DataFrame(a=1:2,b=1:2), DataFrame(a=3:4,b=3:4),
           source = :c => [  "df1",   "df2"  ])
4×3 DataFrame
 Row │ a      b      c
     │ Int64  Int64  String
─────┼──────────────────────
   1 │     1      1  df1
   2 │     2      2  df1
   3 │     3      3  df2
   4 │     4      4  df2

@Lincoln-Hannah
Copy link
Author

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants