简体   繁体   中英

Remove first 3 characters in string using a condition statement

Can anyone Kindly help please? I'm trying to remove three of the first characters within the string using the statement:

Data['COUNTRY_CODE'] = Data['COUNTRY1'].str[3:]

This will create a new column after removing the first three values of the string. However, I do not want this to be applied to all of the values within the same column so was hoping there would be a way to use a conditional statement such as 'Where' in order to only change the desired strings?

I assume you are using pandas so your condition check can be like:

condition_mask = Data['COL_YOU_WANT_TO_CHECK'] == 'SOME CONDITION'

Your new column can be created as:

# Assuming you want the first 3 chars as COUNTRY_CODE
Data.loc[condition_mask, 'COUNTRY_CODE'] = Data['COUNTRY1'].str[:3]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM