pandas 跳过时间表中的站点
pandas skip stop in timetable
我正在尝试跳过 pandas 时间表中的一些站点,如下所示:
departure arrival in out
0 a b 1 0
1 b '#delete' 2 0
2 '#delete' d 0 3
3 d e 1 1
我尝试跳过时间表中的#delete 值并加入进出值:
departure arrival in out
0 a b 1 0
1 b d 2 3
2 d e 1 1
有人知道如何实现吗?
编辑:
对 Wen 的解决方案稍作修改对我有用:
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()
像这样(未经测试):
skipfrom = np.where(df.arrival == '#delete')[0]
skipto = skipfrom + 1
df.arrival[skipfrom] = df.arrival[skipto].values
df.out[skipfrom] = df.out[skipto].values
df = df[df.departure != '#delete']
更像是自定义 fillna
问题
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
我正在尝试跳过 pandas 时间表中的一些站点,如下所示:
departure arrival in out
0 a b 1 0
1 b '#delete' 2 0
2 '#delete' d 0 3
3 d e 1 1
我尝试跳过时间表中的#delete 值并加入进出值:
departure arrival in out
0 a b 1 0
1 b d 2 3
2 d e 1 1
有人知道如何实现吗?
编辑: 对 Wen 的解决方案稍作修改对我有用:
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()
像这样(未经测试):
skipfrom = np.where(df.arrival == '#delete')[0]
skipto = skipfrom + 1
df.arrival[skipfrom] = df.arrival[skipto].values
df.out[skipfrom] = df.out[skipto].values
df = df[df.departure != '#delete']
更像是自定义 fillna
问题
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