简体   繁体   中英

Remove specific words string (both prefix and postfix) from a pandas dataframe column

I want to keep the latest rows with the same ID and also the rows that match certain column values.

Sample Input:
ID                  Address
1                   PALLABI- F #1st Floor, SEC #10, Pallabi, MIRPUR
2                   H#22(2nd floor),Extended Rupnagar Area, Pallabi Mirpur, Dhaka.
3                   Uttar khan-F #3rd floor, Kuripara, Dhaka
4                   F-1,H-43,Chalabon,D.khan, Uttarkhan

PREFIX

ID 1 has a word PALLABI- i want to remove that part. Similar goes for ID 3 where Uttar khan- should be removed. After removal Uttar khan or PALLABI part should add to the postfix of the string only if the prefix doesn't contain any of these words.

POSTFIX

Another part is to remove Dhaka at the end of the string.

Output:
ID                  Address
1                   F #1st Floor, SEC #10, Pallabi, MIRPUR
2                   H#22(2nd floor),Extended Rupnagar Area, Pallabi Mirpur
3                   F #3rd floor, Kuripara, Uttar khan
4                   F-1,H-43,Chalabon,D.khan, Uttarkhan

Thanks in advance.

You can apply a function to the column;

def my_function(string): 
    # if your word is the first in the string
    if 'your_word' == string.split()[0]:
        # replace it with your requirement (it can be nothing (''))
        string = string.replace('what/you/need/to/replace', 'replacement') 

    return string

df['column'] = df['column'].apply(my_function)

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