【问题标题】:If element: rather than if element not None won't work [duplicate]如果元素:而不是如果元素不是无将不起作用[重复]
【发布时间】:2022-02-12 00:22:09
【问题描述】:

据我了解,如果没有电子邮件列,这应该只是通过 if 语句,但是当它到达 if 语句时它会抛出 KeyError: None。有人可以帮我理解这条规则吗?谢谢!

def not_isin(df1, df2, phone_col, df2phone_col,website_col, df2website_col,company_col, df2company_col, email_col, df2email_col):
    print("entered not_isin")
    df1 = df1[~df1[phone_col].isin(df2[df2phone_col])]
    df1 = df1[~df1[website_col].isin(df2[df2website_col])]
    df1 = df1[~df1[company_col].isin(df2[df2company_col])]
    if df1[email_col] and df2[df2email_col]:
        df1 = df1[~df1[email_col].isin(df2[df2email_col])]
        return df1
    return df1

【问题讨论】:

  • email_coldf2email_col 的值是多少?
  • 一旦你解决了这个问题,我想你会遇到一个常见的错误“一个系列的真值是不明确的”。不要尝试将数据框用作布尔值。
  • @Barmar email col 是 'email' 而 df2email_col 在这种情况下是 'Email'。我收到这样的电子邮件是因为它们在每个新数据集上都有所不同。 def get_email(df): email = [col if col.lower().startswith('email') else None for col in df][0] return email 然后我将其分配为 email_col = get_email(df1)
  • 我只是想查找列名是否存在于数据框标题中。
  • 应该是if email_col in df1 and df2email_col in df2:

标签: python if-statement


【解决方案1】:

我认为它在df1[email_col] 失败。

显然,如果email_colNone,您甚至会在到达if 语句之前得到KeyError

【讨论】:

  • 谢谢杰罗姆!我的印象是,如果 df1[email_col] 相当于“如果电子邮件列存在于此数据框中,则继续执行 if 语句的逻辑。我错了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2014-04-10
  • 2020-06-17
  • 1970-01-01
相关资源
最近更新 更多