gpt4 book ai didi

python - 奇怪的 If、Elif、Else 行为

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:43 25 4
gpt4 key购买 nike

如果我单独使用以下任何条件,它就会按预期工作。 (有关所有条件,请参阅更大的代码块)。当我期望 (df['ColumnName'] == 'This Value').any(): 触发时,我确实会打印以下异常

打印异常 'ColumName2' 这会打印 3 次,这样就告诉我满足了其中一个条件,它只是没有触发:$

独立工作

if filename.endswith('.csv'):
print 'File is CSV'
df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True)
try:
if (df['ColumnName'] == 'This Value').any():
final_df = df.drop(df.ix[df['ColumnName'] == 'This Value'].index)
data = final_df.sort(ascending=True)
data.to_csv(os.path.join(root, "Edit.csv"), index=False, na_rep="NA")
except Exception as e:
print e

但是当在更大的 ifelifelse 中使用时,似乎都不起作用(甚至 else 也没有被命中)。语法似乎是正确的,而且我在 StackOverflow 上看到的答案似乎都没有涵盖这一点。没有一个条件是相同的,所以没有看到任何问题,我不完全确定是什么导致了问题。

较大的 if、elif、else

if filename.endswith('.csv'):
df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True)
try:
if (df['ColumnName'] == 'That Value').any():
print 'ColumnName and "That Value"'
final_df = df.drop(df.ix[df['ColumnName'] == 'This Value'].index)
data = final_df.sort(ascending=True)
data.to_csv(os.path.join(root, "Edit.csv"), index=False, na_rep="NA")

elif (df['ColumnName2'] == 'This Value').any():
print 'ColumnName2 This Value'
final_df = df.drop(df.ix[df['ColumnName2'] == 'This Value'].index)
data = final_df.sort(ascending=True)
data.to_csv(os.path.join(root, "Edit.csv"), index=False, na_rep="NA")

elif ((df['ColumnName2']=='That Value')).any():
print 'ColumnName2 That Value'
final_df = df[df['Dst Port'] != 'Any']
data = final_df.sort(ascending=True)
data.to_csv(os.path.join(root, "Edit.csv"), index=False, na_rep="NA")

elif ((df['ColumnName']=='This Value')).any():
print 'ColumnName This Value'
final_df = df[df['Service'] != 'Any']
data = final_df.sort(ascending=True)
data.to_csv(os.path.join(root, "Edit.csv"), index=False, na_rep="NA")

else:
print 'No Filtering Applied'
data = df.sort(ascending=True)
data.to_csv(os.path.join(root, "Edit.csv"), index=False, na_rep="NA")
except Exception as e:
print e

修复感谢 AnandSKumars 评论

if filename.endswith('.csv'):
try:
df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True)
if 'ColumnName' in df and (df['ColumnName'] == 'This Value').That Value():
print 'Service and This Value'
final_df = df.drop(df.ix[df['ColumnName'] == 'This Value'].index)
data = final_df.sort(ascending=True)
with open(os.path.join(root,'Edit.csv'), 'a') as f:
data.to_csv(f, index=False, na_rep="NA")
os.remove(os.path.join(root, filename))

elif 'ColumnName2' in df and (df['ColumnName2'] == 'This Value').That Value():
print 'ColumnName2 and This Value'
final_df = df.drop(df.ix[df['ColumnName2'] == 'This Value'].index)
data = final_df.sort(ascending=True)
with open(os.path.join(root,'Edit.csv'), 'a') as f:
data.to_csv(f, index=False, na_rep="NA")
os.remove(os.path.join(root, filename))

elif 'ColumnName2' in df and ((df['ColumnName2']=='That Value')).That Value():
print 'ColumnName2 and That Value'
final_df = df[df['ColumnName2'] != 'That Value']
data = final_df.sort(ascending=True)
with open(os.path.join(root,'Edit.csv'), 'a') as f:
data.to_csv(f, index=False, na_rep="NA")
os.remove(os.path.join(root, filename))

elif 'ColumnName' in df and ((df['ColumnName']=='That Value')).That Value():
print 'Service and That Value'
final_df = df[df['ColumnName'] != 'That Value']
data = final_df.sort(ascending=True)
with open(os.path.join(root,'Edit.csv'), 'a') as f:
data.to_csv(f, index=False, na_rep="NA")
os.remove(os.path.join(root, filename))

else:
os.remove(os.path.join(root, filename))
except Exception as e:
print e

最佳答案

问题很可能是您有一些 csv 文件,但该 csv 文件中没有某些列。因此,将其读入数据帧后,当您尝试执行 df['ColumnName'] 时,会出现错误 - KeyError: 'ColumnName' 。这表明 DataFrame 中不存在 'ColumnName'

但是一旦发生错误,它就会被 try..except block 捕获,因此不会出现任何其他 ifelif 条件火灾,因此您看不到输出。

如果您只想满足其中一个条件,那么您可以检查您尝试获取的列是否是 df 的一部分。示例-

if 'ColumnName' in df and (df['ColumnName'] == 'That Value').any():

其他 if 条件也类似。

关于python - 奇怪的 If、Elif、Else 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32967619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com