Home > Mobile >  delete end of strings in dataframe
delete end of strings in dataframe

Time:07-12

I have a dataset each cell is like that -29 25846 0 they ends with 0 so I want to delete this zero I tried this piece of code but it doesn't work df[df.columns].applymap(lambda x: x.rstrip(x[-1]))

CodePudding user response:

Not having the dataframe, it would be hard to guess what you are dealing with. anyway, code below might give you a better understanding of how to handle such situation:

def strip_zero(value):
  if value[-1] == "0":
    return value[:-1]
  return value
df["sample_column"].apply(strip_zero)

This will strip the ending zeros from the sample_column

CodePudding user response:

it helps to have a reproducible example, but following will work with the assumption that the data is of this format i.e., ending with zero and may/maynot have space after zero

-29 25846 0

df['columns'].replace(r'\d\s*$','', regex=True)
  • Related