2

I have two DataFrames that I append together ignoring the index so the rows from the appended DataFrame remain the same.

One DataFrame index goes from 0 to 200 and the second DataFrame index goes from 0 to 76

After appending them I try to sort it with a .sort_values then .sort_index because I want the same dates to be together but I also want the larger index to be above the smaller index with the same date as shown in the image below from my output. The red and green is correct but not the blue highlight

I think what is happening is that I have the process in reverse. I think I am sorting by index then by Date and the index order just lands randomly.

lookForwardData=lookForwardData.append(lookForwardDataShell, 
ignore_index=True).sort_values("Date",ignore_index=False)

enter image description here

1
  • It would be easier to help you if you posted sample data from both dataframes Commented Mar 13, 2021 at 18:42

1 Answer 1

2

IIUC, You could do sort_values after resetting the index so it sorts on both the Date col and the index (Date ascending and Index descending)

lookForwardData=lookForwardData.append(lookForwardDataShell,ignore_index=True)

output = (lookForwardData.reset_index()
         .sort_values(['Date','index'],ascending=[True,False]).set_index("index"))
Sign up to request clarification or add additional context in comments.

Comments

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.