📜  从 Pandas 中的 Dataframe 中提取所有大写单词

📅  最后修改于: 2022-05-13 01:54:40.612000             🧑  作者: Mango

从 Pandas 中的 Dataframe 中提取所有大写单词

在本文中,我们将讨论从Pandas中的数据框中提取大写单词的各种方法 模块。以下是将用于描述各种方法的数据框:

Python3
# Import pandas library
import pandas
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
# Display dataframe
df


Python3
# Import pandas library
import pandas
 
 
# explict function to check if string
# contains only uppercase characters
def findCap(s):
    for ele in str(s):
        if ord(ele) < 65 or ord(ele) > 90:
            return 0
    return 1
 
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
 
# access each element in
# the dataframe
for i in range(df.shape[1]):
    for ele in df[i]:
 
        # call explicit function
        if findCap(ele):
            print(ele)


Python3
# Import pandas library
import pandas
 
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
 
# access each element in the dataframe
for i in range(df.shape[1]):
   for ele in df[i]:
        
       # use isupper()
       if str(ele).isupper():
            print(ele)


Python3
# Import required modules
import re
import pandas
 
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
 
# access each element in the dataframe
for i in range(df.shape[1]):
   for ele in df[i]:
        if bool(re.match(r'\w*[A-Z]\w*', str(ele))):
              print(ele)


输出:

方法#1:在显式函数中使用ord()方法

创建一个显式函数来检查传递的字符串参数是否包含所有大写字符。在函数中,通过使用它们的 ASCII 值检查每个字符是否是大写字母。现在在数据帧的每个元素上使用该显式函数来识别大写单词并显示它们。

蟒蛇3

# Import pandas library
import pandas
 
 
# explict function to check if string
# contains only uppercase characters
def findCap(s):
    for ele in str(s):
        if ord(ele) < 65 or ord(ele) > 90:
            return 0
    return 1
 
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
 
# access each element in
# the dataframe
for i in range(df.shape[1]):
    for ele in df[i]:
 
        # call explicit function
        if findCap(ele):
            print(ele)

输出:

PANDAS
DATAFRAME
PYTHON

方法#2:使用str()isupper()方法

访问每个 数据帧的元素并使用str()将每个元素转换为字符串,然后在每个元素上应用isupper()方法。从数据框中提取大写单词并显示它们。

蟒蛇3

# Import pandas library
import pandas
 
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
 
# access each element in the dataframe
for i in range(df.shape[1]):
   for ele in df[i]:
        
       # use isupper()
       if str(ele).isupper():
            print(ele)


输出:

PANDAS
DATAFRAME
PYTHON

方法 #3:使用str()方法和正则表达式 模块

访问每个 数据帧的元素并使用str()将每个元素转换为字符串,然后应用正则表达式从数据帧中提取大写单词并显示它们。

蟒蛇3

# Import required modules
import re
import pandas
 
 
# Create dataset
data = [['tom', 'DATAFRAME', '200.00'],
        ['PANDAS', 15, 3.14],
        ['r2j', 14, 'PYTHON']]
 
# Create the pandas DataFrame
df = pandas.DataFrame(data)
 
 
# access each element in the dataframe
for i in range(df.shape[1]):
   for ele in df[i]:
        if bool(re.match(r'\w*[A-Z]\w*', str(ele))):
              print(ele)


输出:

PANDAS
DATAFRAME
PYTHON