Finding a minimum value in a data frame with multiple columns

Question:

I am trying to get the whole row value with column names while looking for a minimum value
Using FROM_ID and TO_ID. Something like this

enter image description here

I have a data frame which looks like this:

enter image description here

I have 43 From_ID values and nearly 16622 TO_ID values.

To keep it simple, I started working with TO_ID==7 and consider all FROM_ID.

Here is my code:

for i in range(1, 44, 1): 
    df_temp =df_A2C.loc[(df_A2C['FROM_ID'] == i) & (df_A2C['TO_ID'] == 1)]
    minValue = df_temp['DURATION_H'].min()
    for j in range(1,len(df_temp)):
        m = df_temp.loc[(df_temp['DURATION_H']==minValue)]
m

OutputTest:

enter image description here

To verfiy the output, I did this:

z=df_A2C.loc[ (df_A2C['TO_ID'] == 1)]
minValue = z['DURATION_H'].min()
print("minimum value in column 'y': " , minValue)

Output Test:

minimum value in column 'y':  0.459994444444444

As you can see, the minimum values (DURATION_H) are different. I think I am making a mistake. Most probably the correct values is 0.459994444444444.

I need the whole row value with all column names while looking for a minimum value
Using FROM_ID for each TO_ID. The output should be in this form.

enter image description here

Any suggestions!

Asked By: LearningLogic

||

Answers:

This df_A2C.loc[ (df_A2C['TO_ID'] == 7)].sort_values('DURATION_H').head(1) give you an output as:

enter image description here

Then, you can use a for loop to check the whole data frame.

Do this:

for i in range(1, len(df_A2C), 1):
    newDF= df_A2C.loc[(df_A2C['TO_ID'] == i)].sort_values('DURATION_H').head(1)
    print(newDF)
Answered By: ExploringAI
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.