Removing characters from a string in pandas

前端 未结 2 869
春和景丽
春和景丽 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 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)
    

提交回复
热议问题