3 回答
TA贡献1804条经验 获得超2个赞
空行填写fillna(method='ffill'),由服务提取,由 获取shift(-1)。这是否符合问题的意图?
df['service'] = df['service'].fillna(method='ffill')
df = df[df['service'] == 'Express']
df[['number','Shipment Date']] = df[['number','Shipment Date']].fillna(method='ffill')
df[['desc','amount']] = df[['desc','amount']].shift(-1)
df
number Shipment Date service desc amount
8 5.733894e+09 29/04/2020 Express DUTIES TAXES PAID 25.00
9 5.733894e+09 29/04/2020 Express FUEL SURCHARGE 3.28
10 5.733894e+09 29/04/2020 Express NaN NaN
14 2.998455e+09 4/5/20 Express FUEL SURCHARGE 0.72
15 2.998455e+09 4/5/20 Express NaN NaN
TA贡献1803条经验 获得超6个赞
从逻辑上讲,您有一个经典的主/详细数据集。您的详细数据集没有主记录的外键。添加 FK,然后您可以对 master 进行过滤条件,对 detail 进行过滤条件并将 FK 加入 PK
已经修改了源数据,使得从中构建 DF 变得简单
填充详细记录的 FK
fillna
选择主记录和明细记录并将它们加入 PK/FK
import numpy as np
data = '''number Shipment Date service desc amount
182692345 2/12/19 DUTIES & TAXES
- - IMPORT EXPORT DUTIES 561.01
- - IMPORT EXPORT TAXES 600.47
1827975839 2/12/19 DUTIES & TAXES
- - IMPORT EXPORT DUTIES 160.19
3229475633 2/12/19 DUTIES & TAXES
- - IMPORT EXPORT TAXES 600.47
- - IMPORT EXPORT DUTIES 561.01
5733894261 29/04/2020 Express
- - DUTIES TAXES PAID 25
- - FUEL SURCHARGE 3.28
1826995520 2/12/19 DUTIES & TAXES
- - IMPORT EXPORT TAXES 600.47
- - IMPORT EXPORT DUTIES 561.01
2998455062 4/5/20 Express
- - FUEL SURCHARGE 0.72'''
da = [[i for i in re.split("[ ][ ]+", l)] for l in data.split("\n")]
dfall = pd.DataFrame(da[1:], columns=da[0])
dfall["number"][dfall["number"]==""] = np.NaN
dfall = dfall.fillna(method="ffill")
pd.concat([dfall[dfall["desc"]=="FUEL SURCHARGE"], dfmaster[dfall["service"]=="Express"] ],
join="inner", keys="number"
).sort_values(by=["number","service"], ascending=[True,False])
TA贡献1783条经验 获得超4个赞
您可以向前填充service
列中的缺失值,然后比较列表中的Express
和最后shift
匹配的行和列 by DataFrame.shift
and DataFrame.loc
:
mask = df['service'].ffill().eq('Express')
df.loc[mask, ['desc','amount']] = df.loc[mask, ['desc','amount']].shift(-1)
print (df)
number Shipment Date service desc \
0 182692345 2/12/19 DUTIES & TAXES
1 NaN NaN IMPORT EXPORT DUTIES
2 NaN NaN IMPORT EXPORT TAXES
3 1827975839 2/12/19 DUTIES & TAXES
4 NaN NaN IMPORT EXPORT DUTIES
5 3229475633 2/12/19 DUTIES & TAXES
6 NaN NaN IMPORT EXPORT TAXES 600.47
7 NaN NaN IMPORT EXPORT DUTIES
8 5733894261 29/04/2020 Express DUTIES TAXES PAID
9 NaN NaN FUEL SURCHARGE
10 NaN NaN
11 1826995520 2/12/19 DUTIES & TAXES
12 NaN NaN IMPORT EXPORT TAXES
13 NaN NaN IMPORT EXPORT DUTIES
14 2998455062 4/5/20 Express FUEL SURCHARGE
15 NaN NaN NaN
amount
0 None
1 561.01
2 600.47
3 None
4 160.19
5 None
6 None
7 561.01
8 25
9 3.28
10 None
11 None
12 600.47
13 561.01
14 0.72
15 NaN
添加回答
举报