Come eliminare il 50% delle righe che condividono un certo valore di colonna

0

Domanda

df.groupby(['target']).count()
Destinazione dati
Negativo 103210
Positivo 211082

Ora, i miei dati positivi è troppo grande. Voglio eliminare il 50% di righe il cui valore nel Target colonna è Positive. Come posso fare?

dataframe drop pandas python
2021-11-24 03:30:28
1

Migliore risposta

6

Per mantenere la metà del Positive righe, sample Il 50% del Positive le righe utilizzando frac=0.5 e drop tali indici:

indexes = df[df.target == 'Positive'].sample(frac=0.5).index
df = df.drop(indexes)

Per mantenere esattamente 100K Positive righe, sample 100K Positive le righe utilizzando n=100_000 e concat con il Negative righe:

df = pd.concat([
    df[df.target == 'Negative'],
    df[df.target == 'Positive'].sample(n=100_000)
])
2021-11-24 04:27:20

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................