从 Pandas 中的 DataFrame 中仅获取大写单词
为了只获取大写单词,我们使用正则表达式。该重模块用在这里和进口。让我们导入所有库-
import re import pandas as pd
创建一个数据帧-
data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'],['KEYBOARD', 'charger', 'SMARTTV', 'camera']] dataFrame = pd.DataFrame(data)
现在,提取大写单词-
for i in range(dataFrame.shape[1]): for ele in dataFrame[i]: if bool(re.match(r'\w*[A-Z]\w*', str(ele))): print(ele)
示例
以下是代码-
import re import pandas as pd # create a dataframe data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'],['KEYBOARD', 'charger', 'SMARTTV', 'camera']] dataFrame = pd.DataFrame(data) # dataframe print"Dataframe...\n",dataFrame print"\nDisplaying only capital words...\n" # extracting capital words for i in range(dataFrame.shape[1]): for ele in dataFrame[i]: if bool(re.match(r'\w*[A-Z]\w*', str(ele))): print(ele)输出结果
这将产生以下输出-
Dataframe... 0 1 2 3 0 computer mobile phone ELECTRONICS electronics 1 KEYBOARD charger SMARTTV camera Displaying only capital words... KEYBOARD ELECTRONICS SMARTTV