我试图跳过如下所示的 Pandas 时间表中的一些站点: departure arrival in out0 a b 1 01 b '#delete' 2 02 '#delete' d 0 33 d e 1 1我尝试跳过时间表中的 #delete 值并加入输入和输出值: departure arrival in out0 a b 1 01 b d 2 32 d e 1 1有谁知道如何实现这一目标?编辑:对文的解决方案稍作修改对我有用: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()
2 回答
烙印99
TA贡献1829条经验 获得超13个赞
更像是自定义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
PIPIONE
TA贡献1829条经验 获得超9个赞
像这样的东西(未经测试):
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']
添加回答
举报
0/150
提交
取消