If I use the any of the following conditions on its own, it works as expected. (see Larger code block for all conditions)
Works on its own
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
But when used in a larger if, elif, else none seem to work (not even the else is hit). The syntax seems to be correct and none of the answer on StackOverflow that I have seen seem to cover this. None of the conditions are the same so don't see any issues there, I'm not entirely sure what is causing the problem.
Larger 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
Aucun commentaire:
Enregistrer un commentaire