熊猫:仅将某些字符串大写

时间:2019-09-13 17:22:18

标签: pandas

我需要在列中大写一些单词,但不是全部。

数据样本:

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

我尝试过的事情:

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

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

错误:

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

1 个答案:

答案 0 :(得分:3)

使用

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