devlops_s
devlops_s

Reputation: 45

How to find the first row with min value of a column in dataframe

I have a data frame where I am trying to get the row of min value by subtracting the abs difference of two columns to make a third column where I am trying to get the first or second min value of the data frame of col[3] I get an error. Is there a better method to get the row of min value from a column[3].

df2 = df[[2,3]]
df2[4] = np.absolute(df[2] - df[3])
#lowest = df.iloc[df[6].min()]
    2   3   4
0   -111    -104    7
1   -130    110     240
2   -105    -112    7
3   -118    -100    18
4   -147    123     270
5   225     -278    503
6   102     -122    224

                2   3   4

desired result = 2 -105 -112 7

Upvotes: 1

Views: 1228

Answers (1)

jezrael
jezrael

Reputation: 862511

Get difference to Series, add Series.abs and then compare by minimal value in boolean indexing:

s = (df[2] - df[3]).abs()
df = df[s == s.min()]

If want new column for diffence:

df['diff'] = (df[2] - df[3]).abs()
df = df[df['diff'] == df['diff'].min()]

Another idea is get index by minimal value by Series.idxmin and then select by DataFrame.loc, for one row DataFrame are necessary [[]]:

s = (df[2] - df[3]).abs()
df = df.loc[[s.idxmin()]]

EDIT:

For more dynamic code with convert to integers if possible use:

def int_if_possible(x):
    try:
        return x.astype(int)
    except Exception:
        return x

df = df.apply(int_if_possible)

Upvotes: 2

Related Questions