简体   繁体   English

如何仅在满足第一个条件时才评估第二个条件?

[英]How let second condition be evaluated only when first one is met?

I made a DataFrame in Pandas and made a DataFrame like below.我在 Pandas 中制作了 DataFrame 并制作了如下所示的 DataFrame。

if (a['conversion'] == line.strip().any() \
    or (a['x'] == line.strip().any() \
    or (a['y'] == line.strip().any() \
    or (a['z'] == line.strip().any():
    generate(line)
    

so basically, I would like to check both keys x, y and z but some types does not have key 'y' or 'z'.所以基本上,我想检查两个键 x、y 和 z,但有些类型没有键 'y' 或 'z'。 So KeyError: 'y' was raised.所以 KeyError: 'y' 被提出。

How can I only approach existing keys?我怎样才能只接近现有的钥匙? or do I have to use try/except?还是我必须使用try/except? or if I have to use.get(['y]) function, how can I implement it?或者如果我必须使用.get(['y]) function,我该如何实现它? (it did not really work) (它并没有真正起作用)

You have two approaches:你有两种方法:

  1. Check if a column exists (Look Before You Leap):检查列是否存在(在您跳跃之前查看):

     if df['conversion'] == line.strip().any() \ or df['x'] == line.strip().any() \ # Checking if the columns are part of the dataframe or ('y' in df.columns and df['y'] == line.strip().any()) \ or ('z' in df.columns and df['z'] == line.strip().any()): generate(line)

    or或者

    columns = ('conversion', 'x', 'y', 'z') for column in columns: # If this column is not present, just keep looking if column not in df: continue if df[column] == line.strip().any(): generate(line) break
  2. Try to access the damn column anyway (Easier to Ask for Forgiveness than Permission):无论如何都要尝试访问该死的列(请求宽恕比许可更容易):

     condition = False columns = ('conversion', 'x', 'y', 'z') for column in columns: try: # Update the condition using the or clause condition = df[column] == line.strip().any() except KeyError: # If the key is not present, just keep looking continue else: # Use short-circuiting if condition: generate(line) break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当满足第二个条件时,第一个条件失败 - When second condition is met, first condition fails 在python中,我如何才能有2个循环,一旦满足给定条件,第二个循环将返回第一个循环? - In python how can I have 2 loops where the second one returns to the first one once a given condition is met? 如果首先评估第二个条件通常会导致错误,那么使用'和'是好的做法 - Is it good practice to use 'and' when the second condition would normally result in an error if evaluated first 如何使其仅在满足我的时间的强加条件时显示 - how to make it be shown only when the imposed condition of my when is met 如何仅在满足条件时压缩 2 个列表中的项目 - python - How to zip items in 2 lists only when a condition is met - python 如何只在满足特定条件时运行的while循环 - How to while loop that only run when certain condition is met 至少满足一个条件时显示行 - Show rows when at least one condition is met 如何仅在满足序列条件时替换行 - How to substitute rows only if a sequence condition is met 仅标记DataFrame中满足条件的第一行 - Flag only first row where condition is met in a DataFrame 仅当满足一个条件并从数据框中的单元格中删除值而不是行本身时,如何才能找到字符串? - How to find string only if one condition is met and drop value from cell in dataframe but not the row itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM