dmeu dmeu - 11 months ago 75
Python Question

Method chaining solution to drop column level in pandas DataFrame

I am using lot's of method chaining when reshaping and querying data I have in pandas

. Sometimes additional and unnecessary levels for the in indices (rows) and columns are being created. If so, e.g. on the the index (row-axis) this is easily solved by using

df.query('some query')
.reset_index('unwanted_index_level',drop=True) # <====

function allows one to continue chain methods and keep working with the

Nevertheless, I never found an equivalent solution for the column_axis. Is there any at all?

Answer Source

You can just stack the column (move it to the index) and call reset_index with drop=True, or you could write a reset_columns() method using the reset_index() one as a starting point (see

df.query('some query')