0

Relatively new to python, I'm trying to use pandas sort_values() to sort my df by a specific column. But I'm getting an error about multiindexing, but I don't think I've created a multiindex have I? Here is the data I'm practicing with. I want to see all data organized by "diggies" in ascending.

df

  letters digits diggies
0       A      1          7
1       B      2          3
2       C      3         20

df.sort_values(by="diggies") ValueError: Cannot sort by column diggies in a multi-index you need to explicitly provide all the levels

Have I unintentionally created a multiindex? Interestingly, I was able to run sort from another practice data set I imported from a CSV. I though the difference might ahve been the data types, but they are both combinations of either "objects" or "int64".

Any tips on why I'm getting this error and how to get around it? Thanks

***added info

MultiIndex(levels=[['digits', 'letters', 'diggies']], labels=[[1, 0, 2]])

2
  • please show us df.columns Commented Dec 3, 2018 at 2:42
  • Just ran df.columns...which does indicate its a multiIndex...why is that and how do I fix it? Commented Dec 3, 2018 at 2:47

1 Answer 1

1

You can using get_level_values to reset the index

df.columns=df.columns.get_level_values(0)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That worked...I'll have to read up somemore to understand how I created multiindex in first place and how .get_level_values() works. I apprecaite the quick response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.