Search code examples
pythonpandasdataframetimetable

pandas skip stop in timetable


I am trying to skip some stops in a pandas timetable which looks like this:

    departure   arrival     in  out
0   a           b           1   0
1   b           '#delete'   2   0
2   '#delete'   d           0   3
3   d           e           1   1

I try to skip the #delete values in the timetable and join the in and out values:

    departure   arrival     in  out
0   a           b           1   0
1   b           d           2   3
2   d           e           1   1

Does anyone know how to achieve this?

EDIT: A little modification of Wen's solution worked for me:

df = df.mask(df=="#delete")
df.arrival = df.arrival.fillna(method='ffill')
df.departure = df.departure.fillna(method='bfill')
df = df.groupby(['arrival', 'departure']).sum()

Solution

  • More like a customize fillna problem

    df=df.mask(df=="'#delete'")
    df.departure=df.departure.ffill()
    
    df.arrival=df.arrival.bfill()
    
    df.groupby(['departure','arrival'],as_index=False).sum()
    Out[761]: 
      departure arrival  in  out
    0         a       b   1    0
    1         b       d   2    3
    2         d       e   1    1