【问题标题】:How to remove rows with special character at the end of string in Python如何在Python中删除字符串末尾带有特殊字符的行
【发布时间】:2020-09-30 04:49:45
【问题描述】:

如果列 'Submitter' 在数据帧的字符串末尾有特殊字符 'X',我想删除行。

 Submitter       Age     Country
 AfiqX           23      Malaysia
 Nur, AthirahX   23      Malaysia
 Nur, Alia       23      Malaysia

在上面的示例数据框中,我想删除第 1 行和第 2 行,因为它在名称末尾包含“X”。

【问题讨论】:

    标签: python dataframe filter


    【解决方案1】:

    你可以使用 str(pandas series)的endswith

    df = df[~df['Submitter'].str.endswith('X')]
    

    【讨论】:

      【解决方案2】:
      df = pd.DataFrame(data)
      print(df[df['Submitter'].str.endswith("X") == False])
      

      这将给出所有不以 X 结尾的行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-16
        • 1970-01-01
        • 1970-01-01
        • 2011-01-04
        • 2011-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多