Lynn
Lynn

Reputation: 4408

Insert $ in front of numbers in column using Pandas

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.

Upvotes: 0

Views: 549

Answers (2)

Bhargav
Bhargav

Reputation: 4092

You can also try

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

Upvotes: 2

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521864

We could use str.replace here:

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

Upvotes: 1

Related Questions