So zählen Sie eindeutige Zustandskombinationen pro ID in einem Polars -DatenframePython

Python-Programme
Anonymous
 So zählen Sie eindeutige Zustandskombinationen pro ID in einem Polars -Datenframe

Post by Anonymous »

Ich habe einen Polars -Datenrahmen, bei dem jede ID mehrmals mit unterschiedlichen Zustandswerten (entweder 1 oder 2) angezeigt wird. Ich möchte zählen, wie viele eindeutige IDs nur den Zustand 1, nur den Zustand 2 oder beide Zustände 1 und 2. < /p>
haben

Code: Select all

import polars as pl

df = pl.DataFrame({
"id": [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 20, 20, 20],
"state": [1, 2, 1, 1, 2, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2]
})
Ich möchte zählen, wie viele eindeutige IDs in jede Kategorie fallen:
• Nur Status 1 (z. B. IDs, die nur 1 haben)
• Beide Zustände 1 und 2 (z. B. IDs, die sowohl 1 als auch 2 haben) < /p>
Erwartete Ergebnisse (Beispiel): < /p>

Code: Select all

State combination [1]  -> 20 IDs
State combination [2]  -> 15 IDs
State combination [1, 2]  -> 30 IDs

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post