Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer becomes decimal in merged dataframe using python pandas

Tags:

pandas

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?

int becomes decimal

like image 841
Hong Avatar asked Aug 26 '17 01:08

Hong


People also ask

How do you convert decimal to INT in pandas?

Use pandas DataFrame. astype() function to convert column to int (integer), you can apply this on a specific column or on an entire DataFrame.


1 Answers

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.

like image 177
unutbu Avatar answered Jan 03 '23 11:01

unutbu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!