Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding down values in Pandas dataframe column with NaNs

I have a Pandas dataframe that contains a column of float64 values:

tempDF = pd.DataFrame({ 'id': [12,12,12,12,45,45,45,51,51,51,51,51,51,76,76,76,91,91,91,91],
                        'measure': [3.2,4.2,6.8,5.6,3.1,4.8,8.8,3.0,1.9,2.1,2.4,3.5,4.2,5.2,4.3,3.6,5.2,7.1,6.5,7.3]})

I want to create a new column containing just the integer part. My first thought was to use .astype(int):

tempDF['int_measure'] = tempDF['measure'].astype(int)

This works fine but, as an extra complication, the column I have contains a missing value:

tempDF.ix[10,'measure'] = np.nan

This missing value causes the .astype(int) method to fail with:

ValueError: Cannot convert NA to integer

I thought I could round down the floats in the column of data. However, the .round(0) function will round to the nearest integer (higher or lower) rather than rounding down. I can't find a function equivalent to ".floor()" that will act on a column of a Pandas dataframe.

Any suggestions?

like image 526
user1718097 Avatar asked Mar 08 '16 17:03

user1718097


1 Answers

You could just apply numpy.floor;

import numpy as np

tempDF['int_measure'] = tempDF['measure'].apply(np.floor)

    id  measure  int_measure
0   12      3.2            3
1   12      4.2            4
2   12      6.8            6
...
9   51      2.1            2
10  51      NaN          NaN
11  51      3.5            3
...
19  91      7.3            7
like image 116
Joachim Isaksson Avatar answered Sep 17 '22 15:09

Joachim Isaksson