Home > Enterprise >  Replacing Special Symbols in Pandas Dataframe
Replacing Special Symbols in Pandas Dataframe

Time:09-23

Can anyone help me to remove extra characters from the dataframe column? Should I have to use replace string method?

For example,

`$7.99` -> `7.99`
`$16.99` -> `16.99`
`$0.99` -> `0.99`

CodePudding user response:

You can remove the first character with .str[1:]:

df["Column1"] = df["Column1"].str[1:].astype(float)
print(df)

Prints:

   Column1
0     7.99
1    16.99
2     0.99

Dataframe used:

  Column1
0   $7.99
1  $16.99
2   $0.99

CodePudding user response:

IIUC: consider the following dataframe:

df = pd.DataFrame(['$7.99', '$3.45', '$56.99'])

you can use replace to do:

df[0].str.replace('$', '', regex=False)

Output:

0     7.99
1     3.45
2    56.99
Name: 0, dtype: object
  • Related