Home > Enterprise >  How do I filter only numbers that contains decimal greater than .00?
How do I filter only numbers that contains decimal greater than .00?

Time:11-29

How do I filter only numbers that contains decimal greater than .00 in python/pandas?

df = pd.DataFrame({
    'Lineitem price': [4.00, 5.65, 1.22, 8.00, 10.78, 7.00, 2.85]
})

   Lineitem price
0            4.00
1            5.65
2            1.22
3            8.00
4           10.78
5            7.00
6            2.85

Basically I would like to keep only the numbers 5.65, 1.22, 10.78, and 2.85. This would be for a much larger DataFrame, so this is only a sample.

CodePudding user response:

Use np.floor to remove the decimal part.

>>> df[df['Lineitem price'] != np.floor(df['Lineitem price'])]
   Lineitem price
1            5.65
2            1.22
4           10.78
6            2.85

CodePudding user response:

You can divide the column by 1 and see what the remainder is.

df['Lineitem price'] % 1 != 0
0    False
1     True
2     True
3    False
4     True
5    False
6     True

x % 1 gives the remainder after dividing by 1, so it gives whatever is to the right of the decimal. Check if it is zero.

CodePudding user response:

get the decimal part of the number this way:

x % 1

after that filter accordingly.

How to get numbers after decimal point?

  • Related