Extract all capital words from Dataframe in Pandas
Last Updated :
26 Mar, 2023
In this article, we are to discuss various methods to extract capital words from a dataframe in the pandas module. Below is the dataframe which is going to be used to depict various approaches:
Python3
import pandas
data = [[ 'tom' , 'DATAFRAME' , '200.00' ],
[ 'PANDAS' , 15 , 3.14 ],
[ 'r2j' , 14 , 'PYTHON' ]]
df = pandas.DataFrame(data)
df
|
Output:
Method #1: Using ord() method in an explicit function
Create an explicit function to check if the string argument passed contains all capital characters or not. In the function check if each character is a capital letter or not by using their ASCII value. Now use that explicit function on each element of the dataframe to identify capital words and display them.
Python3
import pandas
def findCap(s):
for ele in str (s):
if ord (ele) < 65 or ord (ele) > 90 :
return 0
return 1
data = [[ 'tom' , 'DATAFRAME' , '200.00' ],
[ 'PANDAS' , 15 , 3.14 ],
[ 'r2j' , 14 , 'PYTHON' ]]
df = pandas.DataFrame(data)
for i in range (df.shape[ 1 ]):
for ele in df[i]:
if findCap(ele):
print (ele)
|
Output:
PANDAS
DATAFRAME
PYTHON
Method #2: Using str() and isupper() methods
Access each element of the dataframe and convert each element into a string using str(), after that apply isupper() method on each element. Extract the capital words from the dataframe and display them.
Python3
import pandas
data = [[ 'tom' , 'DATAFRAME' , '200.00' ],
[ 'PANDAS' , 15 , 3.14 ],
[ 'r2j' , 14 , 'PYTHON' ]]
df = pandas.DataFrame(data)
for i in range (df.shape[ 1 ]):
for ele in df[i]:
if str (ele).isupper():
print (ele)
|
Output:
PANDAS
DATAFRAME
PYTHON
Method #3: Using str() method and regex module
Access each element of the dataframe and convert each element into a string using str(), after that apply the regex to extract the capital words from the dataframe and display them.
Python3
import re
import pandas
data = [[ 'tom' , 'DATAFRAME' , '200.00' ],
[ 'PANDAS' , 15 , 3.14 ],
[ 'r2j' , 14 , 'PYTHON' ]]
df = pandas.DataFrame(data)
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)
|
Output:
PANDAS
DATAFRAME
PYTHON
Time Complexity: O(n*n)
Auxiliary Space: O(n), where n is length of list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...