1

I can't seem to find an answer on this topic. I am trying to sort the values in my queryset. Right now, it's automatically sorted by TICKER_id:

TICKER_id
DXJ    -0.5
EWA     1.0
EWC     0.0
EWG    -1.0
EWI    -0.5
EWP    -0.5
EWQ     0.5
EWU     0.0
EWW    -0.5
EWY    -1.0
EWZ     0.5
EZA     0.5
FEZ    -0.5
INDA    0.0
MCHI   -0.5
RSX     0.5
SPY     0.0
TUR    -0.5

I feel as if there is a way to sort by iterating through the list using .iterrows().

5
  • what exactly are you asking? Commented Jul 16, 2019 at 20:37
  • you don't have a list, you have some pandas data structure, in which case, you should use the sort_values method Commented Jul 16, 2019 at 20:37
  • you shouldn't be using iterrows working with pandas Commented Jul 16, 2019 at 20:39
  • @juanpa.arrivillaga This data is stored in a list: Commented Jul 16, 2019 at 20:55
  • What you are showing is not a list, and you've tagged this with pandas, so i'm pretty sure it isn't a list. if you want to sort a list, you just use the sorted function. or the .sort method on a list, which will sort in-place Commented Jul 16, 2019 at 20:59

2 Answers 2

7

As far as I can tell, Pandas uses the index to control itterows and will therefore go back to the normal order, even if you've resorted the dataframe, because the index goes with the row.

I've been able to iterate over the df in the intended order by resetting the index:

#%%
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    [
        {"TICKER_id": "DXJ", "val": -0.5},
        {"TICKER_id": "EWA", "val": 1.0},
        {"TICKER_id": "EWC", "val": 0.0},
        {"TICKER_id": "EWG", "val": -1.0},
        {"TICKER_id": "EWI", "val": -0.5},
        {"TICKER_id": "EWP", "val": -0.5},
        {"TICKER_id": "EWQ", "val": 0.5},
        {"TICKER_id": "EWU", "val": 0.0},
        {"TICKER_id": "EWW", "val": -0.5},
        {"TICKER_id": "EWY", "val": -1.0},
        {"TICKER_id": "EWZ", "val": 0.5},
        {"TICKER_id": "EZA", "val": 0.5},
        {"TICKER_id": "FEZ", "val": -0.5},
        {"TICKER_id": "INDA", "val": 0.0},
        {"TICKER_id": "MCHI", "val": -0.5},
        {"TICKER_id": "RSX", "val": 0.5},
        {"TICKER_id": "SPY", "val": 0.0},
        {"TICKER_id": "TUR", "val": -0.5},
    ]
)

df.plot() # 1

#%%
df.sort_values(by="val", ascending=False, inplace=True)
df.reset_index(drop=True, inplace=True)
df.plot() # 2

#%%
for index, row in df.iterrows():
    if len(row.TICKER_id) == 3:
        plt.scatter(index,row.val, c="r")
    else:
        plt.scatter(index,row.val, c="b") # 3

This gives: 1. enter image description here 2. enter image description here 3. enter image description here

This question is related to this.

Sign up to request clarification or add additional context in comments.

Comments

-2

Maybe you just need .sort_values()?

I.e. dat.sort_values() or dat.sort_values(ascending=False)

2 Comments

I've tried that. I am getting a KeyError: 'TICKER_id'. lag_scores.sort_values(by=['TICKER_id'], inplace= True)
You need to get rid of “by=[‘TICKER_id’]” when sorting on a Series.

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.