Removing characters from a string in pandas

前端 未结 2 870
春和景丽
春和景丽 2020-12-20 04:27

I have a similar question to this one: Pandas DataFrame: remove unwanted parts from strings in a column.

So I used:

temp_dataframe[\'PPI\'] = temp_d         


        
相关标签:
2条回答
  • 2020-12-20 04:55

    use replace:

    temp_dataframe['PPI'].replace('PPI/','',regex=True,inplace=True)
    

    or string.replace:

    temp_dataframe['PPI'].str.replace('PPI/','')
    
    0 讨论(0)
  • 2020-12-20 05:12

    use vectorised str.lstrip:

    temp_dataframe['PPI'] = temp_dataframe['PPI'].str.lstrip('PPI/')
    

    it looks like you may have missing values so you should mask those out or replace them:

    temp_dataframe['PPI'].fillna('', inplace=True)
    

    or

    temp_dataframe.loc[temp_dataframe['PPI'].notnull(), 'PPI'] = temp_dataframe['PPI'].str.lstrip('PPI/')
    

    maybe a better method is to filter using str.startswith and use split and access the string after the prefix you want to remove:

    temp_dataframe.loc[temp_dataframe['PPI'].str.startswith('PPI/'), 'PPI'] = temp_dataframe['PPI'].str.split('PPI/').str[1]
    

    As @JonClements pointed out that lstrip is removing whitespace rather than removing the prefix which is what you're after.

    update

    Another method is to pass a regex pattern that looks for the optionally prefix and extract all characters after the prefix:

    temp_dataframe['PPI'].str.extract('(?:PPI/)?(.*)', expand=False)
    
    0 讨论(0)
提交回复
热议问题