Home > Software design >  Replace or delete a character from row in pandas
Replace or delete a character from row in pandas

Time:10-03

Replace or delete a character from the row in pandas I tried using

srno. Name
1 #abc
2 bc#d

'df = df.replace{Name:('#','', regex=True)}'

i want to delete # from name giving me an error

srno. Name
1 abc
2 bcd

df['Name'] = df['Name'].str.replace('#','', regex=True)

delete the other records

srno. Name
1 #abc
2 NAN

CodePudding user response:

Using apply-lambda;

df["Name"] = df["Name"].apply(lambda x: x.replace("#",""))

CodePudding user response:

If row label is Name use:

df.loc['Name'] = df.loc['Name'].str.replace('#','', regex=True)

If Name is column name use:

df = df.replace({'Name':{'#':''}}, regex=True)
print (df)
  srno. Name
0      1  abc
1      2  bcd

df['Name'] = df['Name'].str.replace('#','', regex=True)
print (df)
   srno. Name
0      1  abc
1      2  bcd
  • Related