Stack Overflow Asked by blindside044 on December 2, 2021
I’m trying to figure out how to check an entire column to verify all values are integers, except one, using python pandas. One row name will always have a float num. CSV example:
name, num
random1,2
random2,3
random3,2.89
random4,1
random5,3.45
In this example, let’s say ‘random3’s num will always be a float. So that fact that random5 is also a float, means the program should print an error to the terminal telling the user this.
When the pandas read_csv()
function loads the CSV file into a dataframe, it will assign the float dtype to any column that contains float and integer values. To test if the elements of the column can be expressed exactly as integers, you can use the .is_integer()
method of floats as described in the answer to How to check if float pandas column contains only integer numbers?
In your case, you want to verify that you have only one float in the column, so do this:
import pandas as pd
df = pd.DataFrame({'name':[f"random{i}" for i in range(1,6)], 'num':[2, 3, 2.89, 1, 3.45]})
if sum(~df.num.apply(float.is_integer)) != 1:
print("Error, the data column contains the wrong number of floats!")
If it is possible that the column only contains integers, then the column will have an integer dtype and the above code will cause an error. You could catch the error, or you could also test for this case:
from pandas.api.types import is_float_dtype
if not is_float_dtype(df.num) or sum(~df.num.apply(float.is_integer)) != 1:
print("Error, the data column contains the wrong number of floats!")
Answered by Craig on December 2, 2021
Try this:
if len(df.num.apply(type) == float) >= 2:
print(f"Ups!. There are {len(df.num.apply(type) == float)} float numbers in the column") float numbers in the column")
Each component explanation:
df.num.apply(type) # Generates a series showing the amount of rows per class
(df.num.apply(type) == float) # Derived series sorting only the values with the defined class.
Answered by David Felipe Medina Mayorga on December 2, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP