Home > front end >  Is there any function that can compare numbers in a dataframe column with a range of numbers?
Is there any function that can compare numbers in a dataframe column with a range of numbers?

Time:01-01

I would like to check if a column contains the numbers from 1 to 9. But I don't quite understand how to do it.

My objective is to verify if the column that the user has entered in the input contains the numbers from 1 to 9. If the column contains the numbers from 1 to 9, the output must be: True or False if it does not contain the numbers from 1 to 9 .

The content of the dataFrame is the following:

   1  2  3  4  5  6  7  8  9
A  6  4  7  2  5  9  3  8  1
B  8  9  3  7  6  1  5  2  4
C  5  2  1  8  4  3  9  7  6
D  1  6  8  5  9  4  7  3  2
E  7  3  4  1  2  8  6  9  5
F  2  5  9  6  3  7  1  4  8
G  9  8  2  3  1  5  4  6  7
H  3  1  6  4  7  2  8  5  9
I  4  7  5  9  8  6  2  1  3

Code:

def validateCol(file, n_col):

    T = pd.read_fwf(file, header= None, names=['1','2','3','4','5','6','7','8','9'])
    T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})

    print(T)

    df = pd.DataFrame(T)
    result = df[n_col]
    print(result)

    #1. Validate if the content of n_col has the numbers from 1 to 9.
    #2. Return False or True depending on #1

validateCol(file=input('file name: '), n_col= input('n_col to validate: '))

Expected OUTPUT:

>>> validateCol('file_name', 2)
The column contains the numbers from 1 to 9
True

CodePudding user response:

You can use something like this:

check_list = [*range(1,10)] # [1, 2, 3, 4, 5, 6, 7, 8, 9]

def validateCol(file, n_col):

    T = pd.read_fwf(file, header= None, names=['1','2','3','4','5','6','7','8','9'])
    T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})

    print(T)

    df = pd.DataFrame(T)
    result = all(a in df[n_col].to_list() for a in check_list)
    if result:
        print("Column {} contains the numbers from 1 to 9".format(n_col))
    else:
        print("Column {} does not contain the numbers from 1 to 9".format(n_col))

CodePudding user response:

You're doing a lot of unnecessary stuff in your function, anyway, starting from the result Series:

def validateCol(file, n_col):
    # ...
    target = set(range(1, 10))
    return set(result) >= target
  • Related