1

I am iterating over rows in a dataframe to see if an image is not found on a server. If it's not found, then it removes the row from the dataframe and makes a new CSV file. I would like to take that row that contains the image not found on the server and append it to a new dataframe that will create a new CSV that we can investigate why an image is not found. I can successfully remove the row from the original dataframe, but appending the new row to the new dataframe results in an empty dataframe once run. How might I solve this

filename2 = 'images_not_found.csv'
orders_csv = pd.read_csv(path + filename)
not_found_images = pd.DataFrame()


for index, row in orders_csv.iterrows():
    if not os.path.isfile(row['Image']):
        orders_csv.drop(index, inplace=True)
        not_found_images.append(row, ignore_index=True)

orders_csv.to_csv(path + filename, index=False)
not_found_images.to_csv(path + filename2, index=False)

2 Answers 2

2

Unlike a list, df.append(new_df) doesn't append inline, meaning doesn't add new_df to df. You need to define df again:

df = df.append(new_df)

In your code:

not_found_images = not_found_images.append(row, ignore_index=True)
Sign up to request clarification or add additional context in comments.

Comments

1

It might sound confusing, but the append method of pandas DataFrame doesn't work in place. You have to re-assign the resulting Dataframe to your variable.

not_found_images = not_found_images.append(row, ignore_index=True)

1 Comment

You're welcome. Consider accepting my solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.