Bubnoff
Bubnoff

Reputation: 4097

Pandas: capitalize only certain strings in a column

I need to capitalize some words in a column, but not all.

Data sample:

Name      Dance
CHUNK     Truffle Shuffle
DATA      Gadget Shuffle
MOUTH     Goin Shoppin
COREYH    Aspirator shuffle

What I've tried:

caps_list = ['CHUNK','DATA','MOUTH']

mask = df['Name'].apply(lambda x: x in caps_list)
df['Name'] = df['Name'].mask(mask).capitalize()

Error:

AttributeError: 'Series' object has no attribute 'capitalize'

Upvotes: 1

Views: 79

Answers (1)

BENY
BENY

Reputation: 323226

Using

df.Dance = np.where(df['Name'].isin(caps_list ),df.Dance.str.capitalize(),df.Dance)

Upvotes: 3

Related Questions