[英]Fill np.nan with values based on other columns
I try to match the offer_id
to the corresponding transaction.我尝试将
offer_id
与相应的交易相匹配。 This is the dataset:这是数据集:
time event offer_id amount
2077 0 offer received f19421c1d4aa40978ebb69ca19b0e20d NaN
15973 6 offer viewed f19421c1d4aa40978ebb69ca19b0e20d NaN
15974 6 transaction NaN 3.43
18470 12 transaction NaN 6.01
18471 12 offer completed f19421c1d4aa40978ebb69ca19b0e20d NaN
43417 108 transaction NaN 11.00
44532 114 transaction NaN 1.69
50587 150 transaction NaN 3.23
55277 168 offer received 9b98b8c7a33c4b65b9aebfe6a799e6d9 NaN
96598 258 transaction NaN 2.18
The rule is that when the offer is viewed, the transaction belongs to this offer id.规则是,当查看报价时,交易属于此报价 id。 If the offer is reveived, but not viewed, the transaction does not belong to the offer id.
如果报价已收到,但未查看,则交易不属于报价 ID。 I hope the
time
variable makes it clear.我希望
time
变量能说明问题。 This is the desired result:这是期望的结果:
time event offer_id amount
2077 0 offer received f19421c1d4aa40978ebb69ca19b0e20d NaN
15973 6 offer viewed f19421c1d4aa40978ebb69ca19b0e20d NaN
15974 6 transaction f19421c1d4aa40978ebb69ca19b0e20d 3.43
18470 12 transaction f19421c1d4aa40978ebb69ca19b0e20d 6.01
18471 12 offer completed f19421c1d4aa40978ebb69ca19b0e20d NaN
43417 108 transaction NaN 11.00
44532 114 transaction NaN 1.69
50587 150 transaction NaN 3.23
55277 168 offer received 9b98b8c7a33c4b65b9aebfe6a799e6d9 NaN
96598 258 transaction NaN 2.18
Example code:示例代码:
import pandas as pd
import numpy as np
d = {'time': [0, 6, 6, 12, 12, 108, 144, 150, 168, 258],
'event': ["offer received", "offer viewed", "transaction", "transaction", "offer completed", "transaction", "transaction", "transaction", "offer received", "transaction"],
'offer_id': ["f19421c1d4aa40978ebb69ca19b0e20d", "f19421c1d4aa40978ebb69ca19b0e20d", np.nan, np.nan, "f19421c1d4aa40978ebb69ca19b0e20d", np.nan, np.nan, np.nan, "9b98b8c7a33c4b65b9aebfe6a799e6d9", np.nan]}
df = pd.DataFrame(d)
print("Original data:\n{}\n".format(df))
is_offer_viewed = False
now_offer_id = np.nan
for index, row in df.iterrows():
if row['event'] == "offer viewed":
is_offer_viewed = True
now_offer_id = row['offer_id']
elif row['event'] == "transaction" and is_offer_viewed:
df.at[index, 'offer_id'] = now_offer_id
elif row['event'] == "offer completed":
is_offer_viewed = False
now_offer_id = np.nan
print("Processed data:\n{}\n".format(df))
Outputs:输出:
Original data:
time event offer_id
0 0 offer received f19421c1d4aa40978ebb69ca19b0e20d
1 6 offer viewed f19421c1d4aa40978ebb69ca19b0e20d
2 6 transaction NaN
3 12 transaction NaN
4 12 offer completed f19421c1d4aa40978ebb69ca19b0e20d
5 108 transaction NaN
6 144 transaction NaN
7 150 transaction NaN
8 168 offer received 9b98b8c7a33c4b65b9aebfe6a799e6d9
9 258 transaction NaN
Processed data:
time event offer_id
0 0 offer received f19421c1d4aa40978ebb69ca19b0e20d
1 6 offer viewed f19421c1d4aa40978ebb69ca19b0e20d
2 6 transaction f19421c1d4aa40978ebb69ca19b0e20d
3 12 transaction f19421c1d4aa40978ebb69ca19b0e20d
4 12 offer completed f19421c1d4aa40978ebb69ca19b0e20d
5 108 transaction NaN
6 144 transaction NaN
7 150 transaction NaN
8 168 offer received 9b98b8c7a33c4b65b9aebfe6a799e6d9
9 258 transaction NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.