0

I've been trying to place some new columns in a csv file, but the error "ValueError: cannot reindex from a duplicate axis" appears.

Taking a look at the output, one column is added, but none more. I've googled this for a few hours, and still don't understand the error.

Here is the code:

for filename in [f for f in os.listdir(CSV_DATA_DIR) if isfile(join(CSV_DATA_DIR, f))]:
    print(filename)

    csv_data = pd.read_csv('data.csv')
    csv_input = pd.read_csv(CSV_DATA_DIR + filename

    print(csv_data.index)

    inputViews = csv_input['Views']

    offset = pd.Series([0] * (len(csv_data['Date']) - len(csv_input['Views'])))
    inputViews = inputViews.append(offset)

    if(len(csv_data['Date']) - len(inputViews) == 0):

        csv_data[filename] = inputViews

        csv_data.to_csv('data.csv', index=False)

        print("done")

Here's an image of the CSV file after the program is run if it helps

  • it sounds like you're trying to add columns of the same name. See here: https://stackoverflow.com/questions/27236275/what-does-valueerror-cannot-reindex-from-a-duplicate-axis-mean – anon01 Nov 16 '20 at 04:09
  • 1
    @anon01 Looking at the values I've printed, it seems that they aren't duplicate names. It says that the first video it tries to add is "Video 2017-12-20_2020-11-15 Making a [redacted]" (which is successful), then tries to add "Video 2017-12-21_2020-11-15 Worst [redacted]". – anonymousse Nov 16 '20 at 04:17

0 Answers0