简体   繁体   中英

how to replace string in dataframe on specific column and row?

pandas.DataFrame
({"POC":['K3525','P022','P0025'],
"AREA":['3','',''],
"COUNTRIES":['KR,US,MO','MY,IN,ZW,KR,US','VN,UK,ZW']})
POC AREA COUNTRIES
K3525 3 KR,US,MO
PO22 MY,IN,ZW,KR,US
P0025 VN,UK,ZW

in this DataFrame, i want replace ',ZW' or 'ZW,' to ' '(i mean delete it) at one data(row is 'P0025', column is 'COUNTRIES')

i searched about it, but i can't find any method to set column and row. i found something like

df.replace({'COUNTRIES': {',ZW':'', 'ZW,':''}})

but it doesn't set row and column, just set column only.

desired result is

POC AREA COUNTRIES
K3525 3 KR,US,MO
PO22 MY,IN,ZW,KR,US
P0025 VN,UK

not desired result is

POC AREA COUNTRIES
K3525 3 KR,US,MO
PO22 MY,IN,KR,US
P0025 VN,UK

Use:

m = df['POC'].eq('P0025')

df[m] = df[m].replace({'COUNTRIES': {',ZW':'', 'ZW,':''}})

Or:

 df.loc[m, 'COUNTRIES'] = df.loc[m, 'COUNTRIES'].replace({',ZW':'', 'ZW,':''})

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