Nev1111
Nev1111

Reputation: 1049

Pandas rounding

I have the following sample dataframe:

Market Value
0  282024800.37
1  317460884.85
2 1260854026.24
3  320556927.27
4   42305412.79

I am trying to round the values in this dataframe to the nearest whole number. Desired output:

Market Value

282024800
317460885
1260854026
320556927
42305413

I tried:

df.values.round()

and the result was

Market Value

282025000.00 
317461000.00 
1260850000.00 
320557000.00 
42305400.00 

What am I doing wrong?

Thanks

Upvotes: 0

Views: 81

Answers (3)

kuzzooroo
kuzzooroo

Reputation: 7408

If you like @noah's solution but don't want to only change the pandas display settings temporarily, you can use pandas's built-in context manager for this:

with pd.option_context('display.precision', 0):
    print(df)

and get

   Market value
0     282024800
1     317460885
2    1260854026
3     320556927
4      42305413

Upvotes: 0

noah
noah

Reputation: 2786

The following will keep your data information intact as a float put will have it display/print to the nearest int.

Big caveat: it is only possible to have this apply to ALL dataframes at once (it is a pandas wide option) rather than just a single dataframe.

pd.set_option("display.precision", 0)

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150815

This might be more appropriate posted as a comment, but put here for proper format.

I can't produce your result. With numpy 1.18.1 and Pandas 1.1.0,

df.round().astype('int')

gives me:

   Market Value
0     282024800
1     317460885
2    1260854026
3     320556927
4      42305413

The only thing I can think of is that you may have a 32 bit system, where

df.astype('float32').round().astype('int')

gives me

   Market Value
0     282024800
1     317460896
2    1260854016
3     320556928
4      42305412

Upvotes: 1

Related Questions