我的数据如下所示,其中我尝试使用给定值创建列输出。将2列中的值进行比较,并在熊猫中的第三列中输出结果
a_id b_received c_consumed
0 sam soap oil
1 sam oil NaN
2 sam brush soap
3 harry oil shoes
4 harry shoes oil
5 alice beer eggs
6 alice brush brush
7 alice eggs NaN
生产数据集的代码是
df = pd.DataFrame({'a_id': 'sam sam sam harry harry alice alice alice'.split(),
'b_received': 'soap oil brush oil shoes beer brush eggs'.split(),
'c_consumed': 'oil NaN soap shoes oil eggs brush NaN'.split()})
我希望有一个新的列称为输出看起来像这样
a_id b_received c_consumed output
0 sam soap oil 1
1 sam oil NaN 1
2 sam brush soap 0
3 harry oil shoes 1
4 harry shoes oil 1
5 alice beer eggs 0
6 alice brush brush 1
7 alice eggs NaN 1
所以搜索是如果SAM收到肥皂,油和刷,查找他消费的产品的“消耗”列中的值,所以如果肥皂被消耗,输出将是1,但由于没有消耗刷,输出为0.
同样哈利,他获得石油和鞋,然后寻找石油和鞋的消费列,如果石油消耗时,输出为1
为了使它更清晰,产值相当于第一列(收到),取决于第二列中的值(已消耗)。
我尝试使用此代码
a=[]
for i in range(len(df.b_received)):
if any(df.c_consumed == df.b_received[i]):
a.append(1)
else:
a.append(0)
df['output']=a
这给了我输出
a_id b_received c_consumed output
0 sam soap oil 1
1 sam oil NaN 1
2 sam brush soap 1
3 harry oil shoes 1
4 harry shoes oil 1
5 alice beer eggs 0
6 alice brush brush 1
7 alice eggs NaN 1
的问题是,因为山姆没有消耗刷,输出应该是0,但输出为1 ,因为刷子被不同的人(爱丽丝)消耗掉了。我需要确保不会发生。产出需要针对每个人的消费情况。
我知道这是令人困惑的,所以如果我没有把自己弄的很清楚,请不要问,我会回答你的意见。
你应该包括你到目前为止书面实现这一目标的代码。包含代码会很有帮助,例如某人可以复制并粘贴并创建数据框。 – imp9
另外,谁消耗这个物体有什么关系? – imp9
好吧,我已经添加了代码来重现数据集,是的,它是重要的谁消费它在以后的操作,我想要计算每个用户消费未来收到的项目的可能性。如果情况并非如此,我只是使用'查找'功能 –