简体   繁体   English

检查 pandas 列中的语法是否符合某些条件

[英]check if syntax in pandas column meets certain criteria

I have the dataframe underneath:我在下面有 dataframe:

df = pd.DataFrame(np.array(['YM.296','MM.305','VO.081.019','VO.081.016','AM.081.002.001','AM081','SR.082','VO.081.012.001','VO.081.012.003']))

I want to know in what row the syntax is similar to 'XX.222.333' (example).我想知道语法在哪一行类似于“XX.222.333”(示例)。 So two letters followed by a stop ('.') followed by three numbers followed by a stop ('.') followed by three numbers again.因此,两个字母后跟一个停止符('.'),然后是三个数字,然后是一个停止符('.'),然后是三个数字。

The desired outcome looks as follows:期望的结果如下所示:

tf = pd.DataFrame(np.array([False,False,True,True,False,False,False,False, False]))

Is there a fast and pythonic way to do this?有没有一种快速和pythonic的方法来做到这一点?

You can do that using str.contains and regex.您可以使用str.contains和正则表达式来做到这一点。

As follows:如下:

df[0].str.contains(r'^[A-Z]{2}\.\d{3}\.\d{3}$', regex=True)

Outputs:输出:

0    False
1    False
2     True
3     True
4    False
5    False
6    False
Name: 0, dtype: bool

Here is a visualization of the regex used:这是使用的正则表达式的可视化:

在此处输入图像描述

暂无
暂无

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

相关问题 Python/Pandas:当一列数据满足一定条件时如何处理 - Python/Pandas:How to process a column of data when it meets certain criteria 编写代码以检查密码是否符合特定条件 - Writing code to check if a password meets certain criteria 如果其他列符合条件,则将操作应用于“熊猫”列 - Applying Operation to Pandas column if other column meets criteria 如何查找 Pandas 中每一行的哪一列首先满足条件? - How to find which column meets a criteria first for each row in Pandas? 当行元素符合条件时查找列名 - Find column names when row element meets a criteria Pandas pandas groupby 列并检查组是否满足多个条件 - pandas groupby column and check if group meets multiple conditions 熊猫:计算当前列值和最接近的列值之间的差值,具体取决于它是否满足其他列的条件 - Pandas: Calculating value of difference between current column value and the closest column value depending if it meets criteria at a different column Pandas:如何循环遍历长字符串的每个字符,如果字符满足特定类型和数值条件则执行加法 - Pandas: How to loop through each character of a long string, perform addition if the character meets certain type and numerical criteria Python Pandas:如果groupby中任何前面的行中的值满足特定条件,则从数据框中删除一行 - Python Pandas: Eliminate a row from a dataframe if a value in a any preceding row in a groupby meets a certain criteria 根据某些条件在熊猫中计算新列 - Calculate new column in pandas based on certain criteria
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM