Home > Mobile >  Insert $ in front of numbers in column using Pandas
Insert $ in front of numbers in column using Pandas

Time:11-25

I wish to add dollar symbol in front of all the values in my column.

Data

ID  Price
aa  800
bb  2
cc  300
cc  4

Desired

ID  Price
aa  $800
bb  $2
cc  $300
cc  $4

Doing

df.loc["Price"] ='$'  df["Price"].map('{:,.0f}'.format)

I believe I have to map this, not 100% sure. Any suggestion is appreciated.

CodePudding user response:

We could use str.replace here:

df["Price"] = df["Price"].astype(str).str.replace(r'^', '$', regex=True)

CodePudding user response:

You can also try

df["Price"] = '$'   df["Price"].astype(str)
  • Related