merchmallow
merchmallow

Reputation: 794

Problems casting dtypes

i have a df lke this:

value_1  Value_2
 10.0     20.0

value_1 and value_2 as float64.

How do i cast it to int ad get this:

value_1  Value_2
  10     20

I tried:

   df.astype('int32').dtypes

didn´t work!

Thanks

Upvotes: 0

Views: 35

Answers (2)

SeaBean
SeaBean

Reputation: 23217

As astype() is not updating in place, remember to reassign to df after applying astype():

df = df.astype('int32')

or simply:

df = df.astype(int)

Note: The example shown on official doc of astype() quoting:

Cast all columns to int32: df.astype('int32').dtypes

is only for demo purpose showing the resulting types. It does not effect the changes to original dataframe without reassigning to the original dataframe. (The doc is somewhat misleading to new Pandas users!)

Result:

print(df)

   value_1  Value_2
0       10       20

Upvotes: 1

wwnde
wwnde

Reputation: 26676

Let us try

df.astype(int)



 value_1  Value_2
0       10       20

Upvotes: 1

Related Questions