1 回答
TA贡献1797条经验 获得超6个赞
我相信以下方法正在发挥作用:
mask = df.Sender == 'flag_source'
df[mask] = df.shift()
df.loc[mask, 'Sender'] = 'flag_source'
df.loc[mask, ['Sender_type','Receiver_type']] = 'house'
df = df[~mask.shift(-1).fillna(False).astype(bool)].reset_index(drop=True)
所以步骤是(按行):
制作您需要更改的行的掩码
使用“shift”将这些行设置为等于前一行
将这些行重写
Sender
为flag_source
还重写
Sender_type
和Receiver_type
shift
通过在蒙版上再次使用 a 来删除前面的行。这看起来有点复杂;loc
您还可以对不包含字符串的行执行类似的操作remove
输出:
ID Sender Receiver Date Sender_type Receiver_type Price
0 company A 28 129 2020-04-12 house house 32.0
1 company A flag_source 28 2020-03-20 house house 50.0
2 company B 56 172 2019-02-11 house house 21.0
3 company B 28 56 2019-01-31 house house 23.0
4 company B 312 28 2018-04-02 house house 19.0
5 company C flag_source 61 2020-06-29 house house 52.0
6 company C 78 12 2019-11-29 house house 12.0
7 company C 102 78 2019-10-01 house house 22.0
8 company D 26 98 2020-04-03 house house 61.0
9 company D 101 26 2020-01-30 temp house 53.0
10 company D 96 101 2019-10-18 house temp 19.0
添加回答
举报