79437795

Date: 2025-02-13 21:51:18
Score: 2
Natty:
Report link

For future reference, please provide what the correct output should be instead of just an example output.

You can perform a group by, take the unique States for each ID, then take the value counts of that

combinations = df.group_by('id').agg(pl.col('state').unique())
counts = combinations.select(pl.col('state').value_counts().alias('counts'))
print(counts.unnest('counts'))

assert (counts.select(pl.col('counts').struct.field('count').sum()) == df.n_unique('id')).item()

# Alternatively, as a single expression:
print(df.select(
    pl.col('state').unique().implode()
    .over('id', mapping_strategy='explode')
    .value_counts()
    .struct.unnest()
))

Reasons:
  • RegEx Blacklisted phrase (2.5): please provide what
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: etrotta