在 pandas 表格操作中,很容易多出一列 ‘index’ 列,drop 也 drop 不掉。
问题:
In pandas, after reset_index(), it emerges a new ‘index’ col, but I don’t want it. When I try df.drop(‘index’, axis=1), it tells me [‘index’] not found in axis. I don’t want this ‘index’ col!
whenever doing:
df = df.reset_index()
df = df.rename(columns={‘xx’: ‘yy’})
df = df.sort_values(by=‘xxx’, ascending=False, inplace=True)
When I enter inplace=True or not, or using df = or not, it will show me a new ‘index’ col that I can’t drop.
建议:
When you use reset_index() in pandas, it adds a new column called index by default, which contains the old index values. If you want to drop this new index column after resetting the index, you should use the drop() method with the inplace=True parameter to modify the DataFrame in place, or you can assign the result to a new variable.
Here’s how you can do it:
import