Accessing a Pandas index like a regular column

后端 未结 3 2104
后悔当初
后悔当初 2021-02-19 02:29

I have a Pandas DataFrame with a named index. I want to pass it off to a piece off code that takes a DataFrame, a column name, and some other stuff, and does a bunch of work inv

3条回答
  •  鱼传尺愫
    2021-02-19 03:24

    Instead of using reset_index, you could just copy the index to a normal column, do some work and then drop the column, for example:

    df['tmp'] = df.index
    # do stuff based on df['tmp']
    del df['tmp']
    

提交回复
热议问题