简体   繁体   English

使用条件语句删除字符串中的前3个字符

[英]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? 但是,我不希望将此方法应用于同一列中的所有值,因此希望有一种方法可以使用条件语句(例如“ Where”)来仅更改所需的字符串?

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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