1
votes

Déterminer le nombre premier dans Python DataFrame

J'ai un dataframe comme celui-ci:

      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

et j'ai besoin de savoir si le client respectif est éligible pour un bon ou non. Le critère est que si le trx est un nombre premier, le client est éligible, sinon il n'est pas éligible. Le dataframe devrait être comme ceci:

      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

Je sais comment déterminer le nombre premier mais pas dans un dataframe. Merci d'avance


1 commentaires

écrivez une fonction qui renvoie 'eligible' lorsqu'un nombre est premier sinon 'not eligible' , puis utilisez Series.map(isprime)isprime est le nom de la fonction ou Series.apply(isprime)


3 Réponses :


1
votes

J'ai copié et collé une fonction pour savoir si un nombre est premier à partir d'ici:
Vérificateur de nombres Python Prime

Ensuite, j'utilise .apply () pour appliquer cette fonction à chaque valeur de la colonne 'Trx':

    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

Trame de données résultante:

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)


1 commentaires

Merci! cela fonctionne mais je peaufine un peu la fonction. Au lieu de renvoyer Vrai ou Faux, il renvoie «éligible» ou «non éligible». Juste un petit ajustement mais globalement merci



0
votes

Un moyen un peu plus rapide sera de créer un dictionnaire de nombres premiers entre le min et le max de votre df.Txn et de mapper le dictionnaire sur df.Txn , le remplissage na

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
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'}
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


0 commentaires

1
votes

Pourquoi ne pas utiliser la fonction isprime() de isprime() .

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

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


0 commentaires