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

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

All combinations of the rows in two data frames

问题

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

  1. 使用 DataFrames
  2. a = DataFrame(x1=["a"; "b"; "c"], y1=1:3)
  3. b = DataFrame(x2=["d"; "e"], y2=4:5)
  1. 如何创建一个数据框架 `c`,其中包含 `a` `b` 中行的所有可能组合,即
  2. ```julia
  3. 6×4 数据框架
  4. Row │ x1 x2 y1 y2
  5. │ String String Int64 Int64
  6. ─────┼──────────────────────────────
  7. 1 │ a d 1 4
  8. 2 │ b d 2 4
  9. 3 │ c d 3 4
  10. 4 │ a e 1 5
  11. 5 │ b e 2 5
  12. 6 │ c e 3 5

肯定有比这更优雅的方法

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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)

  1. 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

  1. 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]))

  1. After all, this is quite impractical if I have larger and more complex data frames.
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. 你正在寻找 `crossjoin` 函数:https://dataframes.juliadata.org/stable/lib/functions/#DataAPI.crossjoin
  6. ```julia
  7. julia> crossjoin(a, b)
  8. 6×4 DataFrame
  9. Row │ x1 y1 x2 y2
  10. │ String Int64 String Int64
  11. ─────┼──────────────────────────────
  12. 1 │ a 1 d 4
  13. 2 │ a 1 e 5
  14. 3 │ b 2 d 4
  15. 4 │ b 2 e 5
  16. 5 │ c 3 d 4
  17. 6 │ c 3 e 5
英文:

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

  1. julia&gt; crossjoin(a,b)
  2. 6&#215;4 DataFrame
  3. Row x1 y1 x2 y2
  4. String Int64 String Int64
  5. ─────┼──────────────────────────────
  6. 1 a 1 d 4
  7. 2 a 1 e 5
  8. 3 b 2 d 4
  9. 4 b 2 e 5
  10. 5 c 3 d 4
  11. 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:

确定