Reputation: 629
I've to use pandas v0.16.2 (!) and I'm stuck with some basic formatting.
How can I get rid of the decimal places and apply thousand separator in the 'val' column below. Here is my attempt which still ends up with one decimal.
import pandas as pd
import numpy as np
df = pd.DataFrame({'name':['abc', 'bvn'],'val' : [100000.1234,2000000.1234]})
df['val'] = df['val'].apply(lambda x: '{:,}'.format(np.round(x,0)))
print(df)
Upvotes: 0
Views: 695
Reputation: 8490
the round()
function returns a floating point number, which means you'll still get that decimal in the string representation.
>>> round(3.1)
3.0
An easy way to get rid of the decimal is to cast it to an integer:
>>> int(round(3.1))
3
In your code, this would be:
df = pd.DataFrame({'name':['abc', 'bvn'],'val' : [100000.1234,2000000.1234]})
df['val'] = df['val'].apply(lambda x: '{:,}'.format(int(np.round(x,0))))
print(df)
Upvotes: 2