I just came across a strange phenomenon with Pandas DataFrames, when setting index using DataFrame.set_index('some_index') the old column that was also an index is deleted! Here is an example:
import pandas as pd df = pd.DataFrame({'month': [1, 4, 7, 10],'year': [2012, 2014, 2013, 2014],'sale':[55, 40, 84, 31]}) df_mn=df.set_index('month') >>> df_mn sale year month 1 55 2012 4 40 2014 7 84 2013 10 31 2014 Now I change the index to year:
df_mn.set_index('year') sale year 2012 55 2014 40 2013 84 2014 31 .. and the month column was removed with the index. This is vary irritating because I just wanted to swap the DataFrame index.
Is there a way to not have the previous column that was an index from being deleted? Maybe through something like: DataFrame.set_index('new_index',delete_previous_index=False)
Thanks for any advice
df_mn.reset_index()first which will re-store the month column in the dataframe. Then useset_index.append=Truetoset_index().