两个数据框中行的所有组合

huangapple go评论72阅读模式
英文:

All combinations of the rows in two data frames

问题

以下是翻译好的代码部分:

使用 DataFrames
a = DataFrame(x1=["a"; "b"; "c"], y1=1:3)
b = DataFrame(x2=["d"; "e"], y2=4:5)
如何创建一个数据框架 `c`,其中包含 `a``b` 中行的所有可能组合,即

```julia
6×4 数据框架
 Row │ x1      x2      y1     y2    
     │ String  String  Int64  Int64 
─────┼──────────────────────────────
   1 │ a       d           1      4
   2 │ b       d           2      4
   3 │ c       d           3      4
   4 │ a       e           1      5
   5 │ b       e           2      5
   6 │ c       e           3      5

肯定有比这更优雅的方法

hcat(rename!(DataFrame(Iterators.product(a.x1, b.x2)), [:x1, :x2]), rename!(DataFrame(Iterators.product(a.y1, b.y2)), [:y1, :y2]))

毕竟,如果我有更大更复杂的数据框架,这种方法就不太实用了。


<details>
<summary>英文:</summary>

I have the following two data frames

using DataFrames
a = DataFrame(x1=["a";"b";"c"], y1=1:3)
b = DataFrame(x2=["d";"e"], y2=4:5)


How can I create a data frame `c` that has all possible combinations of rows in `a` and `b`, i.e.

6×4 DataFrame
Row │ x1 x2 y1 y2
│ String String Int64 Int64
─────┼──────────────────────────────
1 │ a d 1 4
2 │ b d 2 4
3 │ c d 3 4
4 │ a e 1 5
5 │ b e 2 5
6 │ c e 3 5


There sure must be something more elegant than

hcat(rename!(DataFrame(Iterators.product(a.x1, b.x2)), [:x1, :x2]), rename!(DataFrame(Iterators.product(a.y1, b.y2)), [:y1, :y2]))

After all, this is quite impractical if I have larger and more complex data frames.

</details>


# 答案1
**得分**: 2

你正在寻找 `crossjoin` 函数:https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.crossjoin

```julia
julia> crossjoin(a, b)
6×4 DataFrame
 Row │ x1      y1     x2      y2
     │ String  Int64  String  Int64
─────┼──────────────────────────────
   1 │ a           1  d           4
   2 │ a           1  e           5
   3 │ b           2  d           4
   4 │ b           2  e           5
   5 │ c           3  d           4
   6 │ c           3  e           5
英文:

you're looking for crossjoin: https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.crossjoin

julia&gt; crossjoin(a,b)
6&#215;4 DataFrame
 Row │ x1      y1     x2      y2
String  Int64  String  Int64
─────┼──────────────────────────────
   1 │ a           1  d           4
   2 │ a           1  e           5
   3 │ b           2  d           4
   4 │ b           2  e           5
   5 │ c           3  d           4
   6 │ c           3  e           5

huangapple
  • 本文由 发表于 2023年7月31日 23:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定