Pandas 熊猫:列中的某些字符串仅大写
我需要在一列中把一些单词大写,但不是全部 数据样本:Pandas 熊猫:列中的某些字符串仅大写,pandas,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'
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'
使用
你的预期产出是多少?三行的所有大写字母?
df.Name.mask(df.Name.isin(caps_list),df.Name.str.capitalize())
或df.Name=np.where(df.Name.isin(caps_list),df.Name.str.capitalize(),df.Name)
对不起,我将编辑这个问题。但是是的,这是预期的输出。@anky_91成功了,谢谢!我走得太远了。
df.Dance = np.where(df['Name'].isin(caps_list ),df.Dance.str.capitalize(),df.Dance)