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

Time:12-25

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

CodePudding user response:

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,':''})
  • Related