2024-05-23 19:01:47 发布
网友
我知道我们可以使用str.contains方法来选择部分字符串。你知道吗
str.contains
我的专栏是
col1 V2648 V9174. V9071 V0021;+ V7615*** ()() random words
我想选择包含V+ 4 digits number模式的所有行。所以我们需要对这些字符串应用多个条件。你知道吗
V+ 4 digits number
我的输出会是
col1 V2648 V9174. V9071 V0021;+ V7615***
str.match
df[df.col1.str.match('[V](\d{4})')] Out[135]: col1 0 V2648 1 V9174. 2 V9071 3 V0021;+ 4 V7615***
你可以做:
mask = df.col1.str.startswith('V') & df.col1.str.contains('\d+') print(df[mask])
输出
col1 0 V2648 1 V9174. 2 V9071 3 V0021;+ 4 V7615***
掩码df.col1.str.startswith('V')检查以'V'开头的所有内容,df.col1.str.contains('\d+')检查具有4数字的所有内容。如果要精确匹配V后跟4位数字,请使用:
df.col1.str.startswith('V')
'V'
df.col1.str.contains('\d+')
4
V
mask = df.col1.str.contains('^V\d+')
str.match
你可以做:
输出
掩码
df.col1.str.startswith('V')
检查以'V'
开头的所有内容,df.col1.str.contains('\d+')
检查具有4
数字的所有内容。如果要精确匹配V
后跟4位数字,请使用:相关问题 更多 >
编程相关推荐