Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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']

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading