如何通过检查另一列来填充一列的值
此图片会更好地帮助:
标题为passengerId的列描述了团体编号和人员编号,同一群体的人通常是一家人,因此他们来自同一个星球。所以它们是 Home Planet 列中的一些 nan 行,我想通过有关 PassengerID 列中的组号的知识来填充它。
因此,我需要一个代码或者可能是一个循环,通过检查他们是否与某人在一个组中来填充“家乡星球”列中的 na 值(因为他们因此可能位于同一个家乡星球,因为他们可能是一个家庭)。这基本上就是我需要的帮助,通过检查组号并使用有关组成员的 Homeplanet 作为 na 值的替换来填充 Homeplanet 列中的 na 值
我尝试运行 for 循环,但我什至不知道指定什么参数。我将 PassengerId 转换为一个数组,与 Homeplanet 相同,并尝试通过成员进行其他操作,但我不知道如何继续。
This image would help better:
The column titled passengerId describes the group number and person number, people in the same group are usually families, hence they come from the same planet. So they are some nan rows in the Home planet column and I want to fill it through knowledge about the group number in the PassengerID column.
So I need a code or maybe a loop that'll fill na values in the Home planet column by checking if they're in a group with someone (because they would therefore be in the same homeplanet since they are likely a family) . That's just basically what I need help with Filling the na values in the Homeplanet column by checking the group number and using the Homeplanet of about group member as the replacement for the na value
I've tried running for loops but I didn't even know what parameter to specify. I converted the PassengerId into an array and the same with Homeplanet and tried to other through members but I didn't know how to move forward.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解描述,此示例数据框将展示问题:
您希望根据
passenger_id
列中的值将NaN
值设置为 3 和 4。您可以通过将 DataFrame 与其经过清理和重复数据删除的自身合并来实现此目的:
问题更新后更新
您可以从
PassengerId
中提取GroupId
字段> 并执行我最初建议的操作:如果您想进行进一步检查以确定这两名乘客是否确实来自同一家庭(例如检查他们的姓名),您可以在
apply
中执行此操作。If I understand the description correctly, this example data frame would showcase the problem:
where you want the
NaN
values to be 3 and 4 based on the value inpassenger_id
column.You can do this with merging the DataFrame with its cleaned and deduplicated self:
Update after the question has been updated
You can extract a
GroupId
field fromPassengerId
and do what I originally suggested like this:If you want to do further checks to determine if the two passengers are indeed from the same family (for example check their names) you can do that in the
apply
.