1

I have dataframe like this:

      Name    Email                  Trx
0   John    john.doe@gmail.com       30
1   Sarah   sarah@gmail.com           7
2   Bob     bob@yahoo.com            11  
3   Chad    chad@outlook.com         21
4   Karen   karen@outlook.com        20
5   Dmitri  dmitri@rocketmail.com    17

and I need to know whether the respective customer eligible for a voucher or not. The criteria is if the trx is a prime number, the customer is eligible, else it's not eligible. The dataframe should be like this:

      Name    Email                  Trx   Voucher
0   John    john.doe@gmail.com       30    not eligible
1   Sarah   sarah@gmail.com           7    eligible
2   Bob     bob@yahoo.com            11    eligible
3   Chad    chad@outlook.com         21    not eligible
4   Karen   karen@outlook.com        20    not eligible
5   Dmitri  dmitri@rocketmail.com    17    eligible

I know how to determine prime number but not in a dataframe. Thank you in advance

Mohammad Iqbal
  • 317
  • 4
  • 13
  • write a function which returns `'eligible'` when a number is prime else `'not eligible'`, then use `Series.map(isprime)` where `isprime` is function name or `Series.apply(isprime)` – Ch3steR Nov 14 '20 at 14:21

3 Answers3

1

I copy and pasted a function to find out if a number is prime from here:
Python Prime number checker

Then I use .apply() to apply this function to every value in column 'Trx':

def isprime(n):
    '''check if integer n is a prime'''

    # make sure n is a positive integer
    n = abs(int(n))

    # 0 and 1 are not primes
    if n < 2:
        return False

    # 2 is the only even prime number
    if n == 2: 
        return True    

    # all other even numbers are not primes
    if not n & 1: 
        return False

    # range starts with 3 and only needs to go up 
    # the square root of n for all odd numbers
    for x in range(3, int(n**0.5) + 1, 2):
        if n % x == 0:
            return False

    return True

df['Voucher'] = df['Trx'].apply(isprime)

Resulting dataframe:

    Name    Email                  Trx  Voucher
0   John    john.doe@gmail.com      30  False
1   Sarah   sarah@gmail.com          7  True
2   Bob bob@yahoo.com               11  True
3   Chad    chad@outlook.com        21  False
4   Karen   karen@outlook.com       20  False
5   Dmitri  dmitri@rocketmail.com   17  True
Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96
  • Thanks! this works but I tweak the function a bit. Instead of returning True or False, it will returns 'eligible' or 'not eligible'. Just minor tweak but overall thanks – Mohammad Iqbal Nov 14 '20 at 14:35
1

Why not use Sympy's isprime() function.

def is_prime(num):
    from sympy import isprime
    return "eligible" if isprime(num) else "not eligible"

df['Voucher'] = df['Trx'].apply(is_prime)
Underoos
  • 4,708
  • 8
  • 42
  • 85
0

A little more faster way will be to create a dictionary of prime numbers within the min and max of your df.Txn and map the dictionary to df.Txn, the filling na

def isPrime(n):
    if n==2: return True
    if n==1 or n%2 == 0: return False
    else:
      for i in range(2, int(n**0.5)+1):
          if n %  i == 0:
            return False
      return True

def get_primes_within(lower,upper):
  prime ={}
  for num in range(lower, upper + 1):
    if isPrime(num):
      prime[num] = 'eligible'
  return prime
prime_dict =  get_primes_within(df.Trx.min(),df.Trx.max())

>>> print(prime_dict)
{7: 'eligible',
 11: 'eligible',
 13: 'eligible',
 17: 'eligible',
 19: 'eligible',
 23: 'eligible',
 29: 'eligible'}
df['Voucher'] = df.Trx.map(prime_dict).fillna('not eligible')
>>> print(df)

     Name                  Email  Trx       Voucher
0    John     john.doe@gmail.com   30  not eligible
1   Sarah        sarah@gmail.com    7      eligible
2     Bob          bob@yahoo.com   11      eligible
3    Chad       chad@outlook.com   21  not eligible
4   Karen      karen@outlook.com   20  not eligible
5  Dmitri  dmitri@rocketmail.com   17      eligible
Shijith
  • 4,602
  • 2
  • 20
  • 34