Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23390

How to create nested list columns in polars

$
0
0

Using group_by() and nest() I can create list columns in R that are very useful for fitting models to multiple datasets.

How can I achieve the same outcome in polars in Python, such that each element of the data column is a small dataframe?

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

enter image description here

df_list$data[[1]]

enter image description here


Viewing all articles
Browse latest Browse all 23390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>