Open stevenlis opened 2 months ago
For now, you can do:
df.with_columns(pl.int_range(0, pl.len()).shuffle().over("group") == 2)
@avimallu This approach doesn't work because IDs have different rows, and it also doesn't subset the dataset at all.
Sorry, you should use filter
, not with_columns
.
@avimallu I think there's been a misinterpretation. This FR is requesting the selection of n random IDs and all associated rows with those IDs. I'm struggling to understand the purpose of your code. I already provided an expected results example above.
Description
Sometimes one may need to sample a dataframe by group. For example, if I have three IDs and I want to randomly select two of them and keep only their rows. I found that some users have tried this in pandas and dplyr, but the idea is to get all unique ID values, sample from them, and then filter the subset.
Consider adding a parameter to the
.sample()
method to simplify this process.