简体   繁体   English

如何在Python中删除字符串末尾带有特殊字符的行

[英]How to remove rows with special character at the end of string in Python

I want to remove rows if the column 'Submitter' has special character 'X' at the end of the string in dataframe.如果'Submitter'列在数据帧中的字符串末尾有特殊字符'X' ,我想删除行。

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

In the above example dataframe, I want to delete rows 1 & 2 as it contains 'X' at the end of the name.在上面的示例数据框中,我想删除第 1 行和第 2 行,因为它在名称末尾包含“X”。

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

df = df[~df['Submitter'].str.endswith('X')]
df = pd.DataFrame(data)
print(df[df['Submitter'].str.endswith("X") == False])

this will give all the rows which don't end with X这将给出所有不以 X 结尾的行

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

相关问题 Python:删除字符串中的特殊字符 - Python: Remove special character in string 如何:在Python中删除特殊字符后的部分Unicode字符串 - How to: remove part of a Unicode string in Python following a special character 如何在不删除空格的情况下删除 python 字符串中的特殊字符“^” - How to remove the special character '^' in a python string without removing whitespace with it Python /从字符串中删除特殊字符 - Python / Remove special character from string 如何检查字符串中是否有特殊字符或数字但不在该字符串的末尾? - How to check if there is a special character or number in string but not at the end of that string? Python:如果字符为 =“/”,则仅从字符串末尾删除字符 - Python: Remove character only from end of string if character is =“/” 如何删除这个特殊字符? - How to remove this special character? Python如何将带有特殊字符的字符串转换为日期时间 - Python how to convert string with special character into datetime 如果在 python 的特定列中找到字符串或特殊字符,则删除行 - Delete rows if found string or special character in a particular column in python 从第一个大写字母字符删除到字符串结尾和特殊字符 - remove from 1st upper case alphabetical character to end of string and and special character
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM