12
ho a che fare con i panda DataFrames come questo:Inserisci dati mancanti panda con precedenti valore non mancante, raggruppati per chiave
id x
0 1 10
1 1 20
2 2 100
3 2 200
4 1 NaN
5 2 NaN
6 1 300
7 1 NaN
Vorrei sostituire ogni NAN 'x' con il precedente non-NAN 'x' da una riga con lo stesso valore 'id':
id x
0 1 10
1 1 20
2 2 100
3 2 200
4 1 20
5 2 200
6 1 300
7 1 300
c'è qualche modo liscia per farlo senza loop manualmente su righe?
L'opzione 'ffill' è quello che mi serve. Grazie! – ChrisB
Puoi anche fare 'df ['x'] = df.groupby ('id'). Fillna (method = 'ffill')' per ottenere lo stesso risultato per una sintassi leggermente più semplice. – Zhang18
@ Zhang18: Grazie per il miglioramento. 'df.groupby (['id']). ffill()' funzionerebbe anche. – unutbu