pandas crashes on repeated DataFrame.reset_index()

Inspecting frame.py, it looks like pandas tries to insert a column ‘index’ or ‘level_0’. If either/both(??) of them are already taken, then it throws the error.

Fortunately, there’s a “drop” option. AFAICT, this drops an existing index with the same name and replaces it with the new, reset index. This might get you in trouble if you have a column named “index,” but I think otherwise you’re okay.

“Fixed” code:

import pandas
A = pandas.DataFrame({
    'val' :  ['aaaaa', 'acaca', 'ddddd', 'zzzzz'],
    'extra' : range(10,14),
})
A = A.reset_index(drop=True)
A = A.reset_index(drop=True)
A = A.reset_index(drop=True)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)