
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Fetch only capital words from DataFrame in Pandas
To fetch only capital words, we are using regex. The re module is used here and imported. Let us import all the libraries −
import re import pandas as pd
Create a DataFrame −
data = [['computer', 'mobile phone', 'ELECTRONICS', 'electronics'],['KEYBOARD', 'charger', 'SMARTTV', 'camera']] dataFrame = pd.DataFrame(data)
Now, extract 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)
Example
Following is the code −
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)
Output
This will produce the following output −
Dataframe... 0 1 2 3 0 computer mobile phone ELECTRONICS electronics 1 KEYBOARD charger SMARTTV camera Displaying only capital words... KEYBOARD ELECTRONICS SMARTTV
- Related Articles
- Fetch some words from the left in MySQL
- Python - Sum only specific rows of a Pandas Dataframe
- MySQL RegExp to fetch records with only a specific number of words
- Python Pandas - Create Multiindex from dataframe
- Python - Cast datatype of only a single column in a Pandas DataFrame
- Python – Strip whitespace from a Pandas DataFrame
- Annotating points from a Pandas Dataframe in Matplotlib plot
- How to add column from another DataFrame in Pandas?
- Fetch substrings from a string with words separated by slash in MySQL?
- Annotate data points while plotting from Pandas DataFrame
- Python - Drop specific rows from multiindex Pandas Dataframe
- Selecting with complex criteria from a Pandas DataFrame
- Python - Remove duplicate values from a Pandas DataFrame
- How to delete a column from Pandas DataFrame
- Python - Select multiple columns from a Pandas dataframe

Advertisements