确定 Python DataFrame 中的素数
Determine Prime Number in Python DataFrame
我有这样的数据框:
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
我需要知道相应的客户是否有资格获得代金券。标准是如果 trx 是素数,则客户符合条件,否则不符合条件。数据框应该是这样的:
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
我知道如何确定质数,但不知道如何在数据框中确定。提前谢谢你
我从这里复制并粘贴了一个函数来判断一个数是否为素数:
Python Prime number checker
然后我使用 .apply() 将此函数应用于列 '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)
结果数据帧:
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
更快一点的方法是在 df.Txn
和 map the dictionary to df.Txn
, the filling na
的 min
和 max
中创建质数字典
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
为什么不使用 Sympy 的 isprime()
功能。
def is_prime(num):
from sympy import isprime
return "eligible" if isprime(num) else "not eligible"
df['Voucher'] = df['Trx'].apply(is_prime)
我有这样的数据框:
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
我需要知道相应的客户是否有资格获得代金券。标准是如果 trx 是素数,则客户符合条件,否则不符合条件。数据框应该是这样的:
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
我知道如何确定质数,但不知道如何在数据框中确定。提前谢谢你
我从这里复制并粘贴了一个函数来判断一个数是否为素数:
Python Prime number checker
然后我使用 .apply() 将此函数应用于列 '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)
结果数据帧:
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
更快一点的方法是在 df.Txn
和 map the dictionary to df.Txn
, the filling na
min
和 max
中创建质数字典
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
为什么不使用 Sympy 的 isprime()
功能。
def is_prime(num):
from sympy import isprime
return "eligible" if isprime(num) else "not eligible"
df['Voucher'] = df['Trx'].apply(is_prime)