Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing lower case letter in column of Pandas dataframe

Tags:

python

pandas

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)
like image 801
notverygood Avatar asked Mar 11 '20 11:03

notverygood


People also ask

How do I change a column name in lowercase in pandas?

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.

How do you remove punctuation from a column in pandas?

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'] .


Video Answer


2 Answers

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
like image 155
Willem Van Onsem Avatar answered Oct 27 '22 07:10

Willem Van Onsem


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.

like image 2
Nicolas Gervais Avatar answered Oct 27 '22 08:10

Nicolas Gervais