【问题标题】:Filter pandas dataframe if value of column is within a string如果列的值在字符串中,则过滤熊猫数据框
【发布时间】:2021-08-26 17:47:16
【问题描述】:

我仔细阅读了Select by partial string from a pandas DataFrame 的帖子,但我认为它并没有解决我的问题。如果可以在字符串中找到行值,我需要过滤数据框的行。

例如,我的桌子是:

Part_Number
A1127
A1347

如果列值在字符串ZA1127B.48 内,我想过滤记录。过滤后的数据框应包含行1。 (所有帖子都显示了如何检查行值是否包含字符串。)

【问题讨论】:

    标签: python pandas dataframe filter


    【解决方案1】:

    您可以使用.apply + in 运算符:

    s = "ZA1127B.48"
    
    print(df[df.apply(lambda x: x.Part_Number in s, axis=1)])
    

    打印:

      Part_Number
    0       A1127
    

    【讨论】:

      【解决方案2】:

      我认为使用 apply 功能会帮助你做你想做的事。

      试试这条线:

      df[df["Part_Number"].apply(lambda x: x in "ZA1127B.48")]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-03-04
        • 2018-01-02
        • 2023-03-14
        • 2015-09-24
        • 2022-07-01
        • 2018-10-04
        • 2018-10-01
        • 1970-01-01
        相关资源
        最近更新 更多