check all items in csv column except one [python pandas]

大兔子大兔子 提交于 2021-02-10 05:56:07

问题


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.


回答1:


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.



回答2:


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!")


来源:https://stackoverflow.com/questions/63043034/check-all-items-in-csv-column-except-one-python-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!