I have two csv file and all the numeric fields are int, no decimal. When I use pandas merge function to join two dataframe, I found the int fields in one dataframe all became decimal, why that happens?
Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame.
Each column of a DataFrame has a dtype. The dtype controls what kinds of values can be contained in that column. Columns with integer dtypes, unsurprisingly, can contain only integers. Columns with floating point dtypes contain only floats -- and NaN
is a float:
In [191]: isinstance(np.nan, float)
Out[191]: True
So even though age
and score
are integer-valued columns, since the merged age_y
and score_y
columns contain NaN
, the dtype must be upgraded to a floating point dtype to accommodate the NaN
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With