简体   繁体   English

从Python中的一列字符串中提取连续的大写单词

[英]Extract consecutive uppercase words from a column of strings in Python

I have a column of strings from which I wish to extract all consecutive uppercase words that appear in different cases.我有一列字符串,我希望从中提取出现在不同情况下的所有连续大写单词。 Here is an example of the type of strings I have:这是我拥有的字符串类型的示例:

data = pd.DataFrame({
    'strings': ['ubicado en QUINTA CALLE, LADO NORTE detras',
                'encuentra por AVENIDA NORTE, ARRIBA DE IGLESIA frente a',
                'direccion en CENTRO COMERCIAL, SEGUNDO NIVEL junto a']
})

The lowercase words appear frequently enough to use them as regular expressions.小写单词出现的频率足以将它们用作正则表达式。 Here is an example of what I have done so far:这是我到目前为止所做的一个例子:

df['extraction'] = df['strings'].str.extract('(?:(?<=ubicado en )|(?<=encuentra por )|(?<=direccion en ))(.*?)(?:(?=\s*detras)|(?=\s*frente\s*a)|(?=\s*junto\s*a))')

However, I would like to find a way to only use the first lowercase words and then apply a regex that would grab all the consecutive uppercase words.但是,我想找到一种方法,只使用第一个小写单词,然后应用一个正则表达式来抓取所有连续的大写单词。

Here is an example of what I am looking for:这是我正在寻找的示例:

df['extraction'] = df['strings'].str.extract('(ubicado\s*en\s*<REGEX>|encuentra\s*por\s*<REGEX>|direccion\s*en\s*<REGEX>)')

This should result in the following:这应该导致以下结果:

data = pd.DataFrame({
    'extraction': ['QUINTA CALLE, LADO NORTE',
                'AVENIDA NORTE, ARRIBA DE IGLESIA',
                'CENTRO COMERCIAL, SEGUNDO NIVEL']
})

The strings are actually much longer and complex texts, so I cannot simply remove all lowercase letters in the column.字符串实际上是更长且更复杂的文本,因此我不能简单地删除列中的所有小写字母。

You can use the following:您可以使用以下内容:

words = '(?:ubicado|encuentra|direccion)'
regex = words+'[^A-Z]*([^a-z]+)'

data['extraction'] = data['strings'].str.extract(regex)

Output:输出:

                                                   strings                         extraction
0               ubicado en QUINTA CALLE, LADO NORTE detras          QUINTA CALLE, LADO NORTE 
1  encuentra por AVENIDA NORTE, ARRIBA DE IGLESIA frente a  AVENIDA NORTE, ARRIBA DE IGLESIA 
2     direccion en CENTRO COMERCIAL, SEGUNDO NIVEL junto a   CENTRO COMERCIAL, SEGUNDO NIVEL 

Or, to avoid trailing non-letter characters:或者,为了避免尾随非字母字符:

words = '(?:ubicado|encuentra|direccion)'
regex = words+'[^A-Z]*([^a-z]*[A-Z]+)'

data['extraction'] = data['strings'].str.extract(regex)

Output:输出:

                                                   strings                        extraction
0               ubicado en QUINTA CALLE, LADO NORTE detras          QUINTA CALLE, LADO NORTE
1  encuentra por AVENIDA NORTE, ARRIBA DE IGLESIA frente a  AVENIDA NORTE, ARRIBA DE IGLESIA
2     direccion en CENTRO COMERCIAL, SEGUNDO NIVEL junto a   CENTRO COMERCIAL, SEGUNDO NIVEL

You could use a custom function to check each character for upper caps.您可以使用自定义函数来检查每个字符的大写字母。 Then use the lambda function on each cell in the DataFrame:然后对 DataFrame 中的每个单元格使用 lambda 函数:

import pandas as pd
    
def check_upper(str):
    output = []
    for x in str:
        if x.isupper() or x==" " or x==",":
            output.append(x)
    return "".join(output).strip().strip(",")

data = pd.DataFrame({
    'strings': ['ubicado en QUINTA CALLE, LADO NORTE detras',
                'encuentra por AVENIDA NORTE, ARRIBA DE IGLESIA frente a',
                'direccion en CENTRO COMERCIAL, SEGUNDO NIVEL junto a']
})
data["strings"] = data["strings"].apply(lambda x: check_upper(x))
data

Output:输出:

    strings
0   QUINTA CALLE, LADO NORTE
1   AVENIDA NORTE, ARRIBA DE IGLESIA
2   CENTRO COMERCIAL, SEGUNDO NIVEL

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM