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
I have a data frame
which looks like this:
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:
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.
Any suggestions!
CodePudding user response:
This df_A2C.loc[ (df_A2C['TO_ID'] == 7)].sort_values('DURATION_H').head(1)
give you an output as:
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)