【问题标题】:Python: How do I check if the last three number of a variable to be 000 or not?Python:如何检查变量的最后三个数字是否为 000?
【发布时间】:2021-03-13 02:54:54
【问题描述】:

结合 excel,我正在寻找一种解决方案,通过实施 if 语句来检查公司代码是否以 000 结尾(任何其他)。输出应该是 True 或 false 语句。我不知道如何确切地开始。想过用switch,但是不知道switch之后怎么实现一个函数或者action。

例如: Excel Chart

示例: 如果代码以 000 结尾:true 如果代码以 001 结尾:false

【问题讨论】:

    标签: python excel pandas xlsxwriter


    【解决方案1】:

    使用Series.str.endswith 测试某些列,这里是column

    #if necessary convert file to DataFrame
    df = pd.read_excel(file)
    
    df['test'] = df['column'].str.endswith('000')
    

    【讨论】:

    • df是Pandas下的函数吗?
    • @Joe - 不,dfpandas DataFrame
    • 我该如何将它实现到带有 if 语句的 for 循环中,以便我可以将 true 或 false 写入不同的列?
    • @Joe - 我认为在 pandas 中使用 for 是一个很大的错误,因为它不是为它设计的,也许可以查看一些教程 here。在使用面具的熊猫中,像这里df['column'].str.endswith('000') 是由True/False 填充的面具
    • @Joe - 但是如果想使用xlsxwriter.Workbook 会更复杂而且我从不使用它,我猜想有一些循环是必要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2014-10-03
    • 2017-01-21
    • 2016-08-22
    • 2012-06-27
    • 2011-02-03
    相关资源
    最近更新 更多