How to loop through a pandas data frame using a columns values as the order of the loop?

Question:

I have two CSV files which I’m using in a loop. In one of the files there is a column called "Availability Score"; Is there a way that I can make the loop iterate though the records in descending order of this column? I thought I could use Ob.sort_values(by=['AvailabilityScore'],ascending=False) to change the order of the dataframe first, so that when the loop starts in will already be in the right order. I’ve tried this out and it doesn’t seem to make a difference.

# import the data 
CF = pd.read_csv (r'CustomerFloat.csv')
Ob = pd.read_csv (r'Orderbook.csv')

# Convert to dataframes
CF = pd.DataFrame(CF)
Ob = pd.DataFrame(Ob)

#Remove SubAssemblies
Ob.drop(Ob[Ob['SubAssembly'] != 0].index, inplace = True)

#Sort the data by thier IDs 
Ob.sort_values(by=['CustomerFloatID'])
CF.sort_values(by=['FloatID'])

#Sort the orderbook by its avalibility score
Ob.sort_values(by=['AvailabilityScore'],ascending=False)


# Loop for Urgent Values
for i, rowi in CF.iterrows():
    count = 0
    urgent_value = 1
    for j, rowj in Ob.iterrows():
        if(rowi['FloatID']==rowj['CustomerFloatID'] and count < rowi['Urgent Deficit']):
            Ob.at[j,'CustomerFloatPriority'] = urgent_value
            count+= rowj['Qty']

Asked By: jhew123

||

Answers:

sort_values() (like most Pandas functions nowadays) are not in-place by default. You should assign the result back to the variable that holds the DataFrame:

Ob = Ob.sort_values(by=['CustomerFloatID'], ascending=False)
# ...

BTW, while you can pass inplace=True as argument to sort_values(), I do not recommend it. Generally speaking, inplace=True is often considered bad practice.

Answered By: Pierre D

You need to add inplace=True, like this:

Ob.sort_values(by=['AvailabilityScore'],ascending=False, inplace=True)
Answered By: gtomer
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.