I have the following df
data = {'Name':['TOMy', 'NICKs', 'KRISHqws', 'JACKdpo'], 'Age':[20, 21, 19, 18]}
How can I remove lowercase letters from the Name column such that when looking at data['Name']
, I have TOM,NICK,KRISH,JACK
.
I tried the following but no luck,
data['Name'].mask(data['Name'].str.match(r'^[a-z]+$'))
data['Name'] = data['Name'].str.translate(None,string.ascii_lowercase)
We can convert the names into lower case using Pandas' str. lower() function. We first take the column names and convert it to lower case. And then rename the Pandas columns using the lowercase names.
To remove punctuation with Python Pandas, we can use the DataFrame's str. replace method. We call replace with a regex string that matches all punctuation characters and replace them with empty strings. replace returns a new DataFrame column and we assign that to df['text'] .
You can update the name
column by making use of .str.replace(..)
[pandas-doc]:
df['Name'] = df['Name'].str.replace('[a-z]', '')
For the given sample data, this gives us:
>>> df['Name'].str.replace('[a-z]', '')
0 TOM
1 NICK
2 KRISH
3 JACK
Name: Name, dtype: object
You can try this too if you don't like RegEx:
df['Name'].apply(lambda x: ''.join([letter for letter in x if letter.isupper()]))
For all rows in the Name
column, concatenate all letters that are uppercase.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With