Home > Enterprise >  How to display the rows in a float column that contains decimals?
How to display the rows in a float column that contains decimals?

Time:11-11

I have a data frame that has a quantity column and I need to check if the data was inputted correctly. If there's a number that was inputted with decimals I receive de data frame with de quantity column as a float and not as an integer, but I need to print out the index of the rows that were inputted with decimals.

For example

data = {'quantity':  [2.00, 1.00, 3.00, 4.55, 5.00, 6.22]
        }

testdf = pd.DataFrame(data)

I need to print out the index of the rows that contain the decimal values that are not .00

CodePudding user response:

You can compare the values to the rounded versions of the values and use boolean masking to select only those rows

testdf[testdf['quantity']!=testdf['quantity'].round()]\
.index\
.tolist()

[3, 5]

CodePudding user response:

One possibility is to use the modulo operator, as in this example, and then extract the indexes that satisfy the condition to a list:

print(testdf[(testdf.quantity % 1) != 0].index.tolist())

[3, 5]
  • Related