如何在循环中根据条件将图像保存到其他目录

时间:2019-07-15 17:42:51

标签: python

我正在尝试根据条件将不同的图像保存到不同的目录。图像在for循环中生成。什么代码可以做到这一点?

我尝试放置一个if语句,但这没用

import matplotlib.pyplot as plt
df = pd.read_excel('file.xlsx')

for i in range(z):
  fig, ax = plt.subplots()
  plt.show()

  #Conditions
  df.iloc["column"] = 1  ##Condition 1
  df.iloc["column"] = 2  ##Condition 2

  if df.iloc["column"] = 1
       #puts image in directory 1 when condition 1 is met
       elseif  if df.iloc["column"] = 2 #puts image in directory 2 when condition 2 is met

#This saves the file to a particular file directory
directory1 = "file path to directory 1"
directory2 = "file path to directory 2"

fig.savefig(directory1 + str(i) +".png")
fig.savefig(directory2 + str(i) +".png")

我希望输出根据条件1和条件2将图像保存在2个文件夹中的每一个中

1 个答案:

答案 0 :(得分:1)

if语句中有很多语法错误;这是更正后的语句:

if df.iloc["column"] == 1:
   #puts image in directory 1 when condition 1 is met
elif df.iloc["column"] == 2:
   #puts image in directory 2 when condition 2 is met

第一个错误:缩进(elif必须在if的同一列下)。
第二个错误:=代替了==
第三个错误:用elseif代替elif