【问题标题】:Incorrect results when using "in" operator with Pandas series?在 Pandas 系列中使用“in”运算符时结果不正确?
【发布时间】:2017-08-27 05:55:40
【问题描述】:

我有两个数据框 df1 包含 100K 行,df2 包含 600 万行。我想修复 'SoftDel???' 的值当 df2 中的“id”匹配时,df1 中的列。代码正在运行,但结果错误。

我已经使用合并完成了这项任务,结果令人满意,但想知道为什么下面会产生错误的结果?

for x, y in df1.iterrows():
if y['id'] in df2['id']: df1.loc[x,'SoftDel???'] = 'No'

【问题讨论】:

    标签: python pandas series


    【解决方案1】:

    y['id'] 是一个浮点数。但是,df2['id'] 是一个系列。 in 运算符并非设计用于将系列作为参数之一。

    【讨论】:

    • 谢谢,但我试过这个并抛出错误:AttributeError: 'int' object has no attribute 'values'
    • @AnkitSaini 没关系,这是一个简单的错误。检查我的编辑。
    • @coldspeed: for x, y in df1.iterrows(): if df2['id'].isin(y['id']).any(): df1.loc[x, 'SoftDel???'] = "No" 在上面使用时得到这个:“TypeError: only list-like objects are allowed to be pass to isin(), you pass a [int]” 当使用这个@ 987654325@ got : AttributeError: 'int' object has no attribute 'isin'
    • @AnkitSaini if df2['id'].isin([y['id']]).any()
    • @ColdSpeed:谢谢它的工作。但是花了一个多小时,所以认为合并方法更好,可以在不到 10 分钟的时间内完成同样的事情。再次感谢您的帮助!!!
    猜你喜欢
    • 2018-08-29
    • 2018-04-21
    • 1970-01-01
    • 2021-10-12
    • 2017-04-10
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多