使用 group_by() 和 Nest() 我可以在 R 中创建列表列,这对于将模型拟合到多个数据集非常有用。
如何在 Python 中的极坐标中实现相同的结果,使得数据列的每个元素都是一个小数据框?
library(tidyverse)
df <- structure(list(dataset_id = c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L),
day = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), recipe = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L), levels = c("A", "B"), class = "factor"),
cum_trials = c(1000, 2000, 1000, 2000, 1000, 2000, 1000,
2000), cum_events = c(644L, 1287L, 643L, 1262L, 645L, 1312L,
655L, 1301L), cum_rate = c(0.644, 0.643, 0.643, 0.619, 0.645,
0.667, 0.655, 0.646)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L))
df_list <- df |> group_by(dataset_id, day) |> nest()
df_list
df_list$data[[1]]
import polars as pl
df = pl.DataFrame({
'a':[1,1,2,2],
'b':[3,4,5,6],
'c':[7,8,9,0],
'string': ["hey", "you", "kids", "go"]
})
df.with_columns(my_list_col = pl.struct('b', 'string'))
a b c string my_list_col
i64 i64 i64 str struct[2]
1 3 7 "hey" {3,"hey"}
1 4 8 "you" {4,"you"}
2 5 9 "kids" {5,"kids"}
2 6 0 "go" {6,"go"}
df.group_by('a').agg(list_col = pl.struct(cs.integer()))
a list_col
i64 list[struct[2]]
1 [{3,"hey"}, {4,"you"}]
2 [{5,"kids"}, {6,"go"}]