2

我正在尝试开发一个 IF 语句,如果 [Column Name] 中有等于零的值,它将运行另一个 python 脚本。- 否则什么都不做。

我最初的想法是做类似的事情

如果 df['列名'] == 0:

subprocess.call("python script.py", shall = True)

别的:

print('No values of 0')

这给了我以下错误:ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

如果我尝试指定其中任何一个,我并没有真正得到我想要的。

具体来说,我希望脚本遍历特定列的值,并查看这些值中的任何一个是否 = 0,如果它们是,我想运行另一个向我发送电子邮件警告的脚本。

抱歉,如果这已经在其他地方解释过,但我找不到。

我在 Python 3.7.5 上并使用熊猫。

感谢您的帮助

4

2 回答 2

2

您需要使用.any来计算整个系列,因为您希望它等于True如果任何值等于 0

df = pd.DataFrame({'count' : [0,1,2,3]})

print(df)

   count
0      0
1      1
2      2
3      3

if df['count'].eq(0).any():
    print('do sth')
else:
    print('pass')

out:

do sth
于 2020-03-29T13:29:57.810 回答
-1

我这里有两个片段可以帮助你:

import pandas as pd
import numpy as np

df = pd.DataFrame()
df['Random_Numbers'] = np.random.randn(10)

第一个选项:

# First: filter the list, check if it's empty, send a single email. 

if df[df['Random_Numbers'] > 0.0].empty == False:
    print('Sending Email....Email Sent')

输出:

"Sending Email....Email Sent"

------------------------------------------------------------------------------

第二种选择:

# Second: iterate over each row in the dataframe, like you mentioned, check the value, send an email zero to multiple times. 

for index, row in df.iterrows():
    if row['Random_Numbers'] > 0.0:
        print('Sending Email...Email Sent')  

输出:

"Sending Email...Email Sent"
"Sending Email...Email Sent"
"Sending Email...Email Sent"
"Sending Email...Email Sent"
"Sending Email...Email Sent"
于 2020-03-29T13:27:31.657 回答