TravisVOX
TravisVOX

Reputation: 21631

Updating DataFrame Value

With this DataFrame called data...

                    winner
 runner_name                         
 Steve                   0
 Joe                     0
 Marsha                  1
 Kent                    0

if I create a new column with a placeholder of 0...

 data['weighted'] = 0

When I pass a decimal value to update it in a loop (after a long series of calculations and other stuff)...

 data.ix[runner_name]['weighted'] = 45.88

...it automatically rounds the figure. Thus, the frame becomes..

                    winner  weighted
 runner_name                         
 Steve                   0        46
 Joe                     0        80
 Marsha                  1       100
 Kent                    0        80

How do I prevent it from rounding?

Upvotes: 0

Views: 126

Answers (1)

unutbu
unutbu

Reputation: 879103

Assigning a float will cause Pandas to give the column a float dtype:

data['weighted'] = 0.0

import pandas as pd
data = pd.DataFrame({'winner':[0,0,1,0]}, index=pd.Series(['Steve', 'Joe', 'Marsha', 'Kent'], name='runner_name'), )
data['weighted'] = 0.0
data.ix['Steve', 'weighted'] = 45.88
print(data)

yields

             winner  weighted
runner_name                  
Steve             0     45.88
Joe               0      0.00
Marsha            1      0.00
Kent              0      0.00

>>> print(data.dtypes)
winner        int64
weighted    float64
dtype: object

Upvotes: 2

Related Questions