Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the integer portion of a float column in pandas

Suppose I have a dataframe df as shown below

    qty
0   1.300
1   1.909

Now I want to extract only the integer portion of the qty column and the df should look like

   qty
0   1
1   1

Tried using df['qty'].round(0) but didn't get the desired result as it rounds of the number to the nearest integer.

Java has a function intValue() which does the desired operation. Is there a similar function in pandas ?

like image 980
Gopal Chandak Avatar asked Jan 25 '26 13:01

Gopal Chandak


1 Answers

Convert values to integers by Series.astype:

df['qty'] = df['qty'].astype(int)
print (df)
   qty
0    1
1    1

If not working above is possible use numpy.modf for extract values before .:

a, b = np.modf(df['qty'])
df['qty'] = b.astype(int)
print (df)
   qty
0    1
1    1

Or by split before ., but it should be slow if large DataFrame:

df['qty'] = b.astype(str).str.strip('.').str[0].astype(int)

Or use numpy.floor:

df['qty'] = np.floor(df['qty']).astype(int)
like image 176
jezrael Avatar answered Jan 27 '26 02:01

jezrael



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!