Maximilian
Maximilian

Reputation: 8480

Selecting the value in a row closest to zero in a pandas DataFrame

We have a pandas DataFrame with two columns:

pd.DataFrame(pd.np.arange(12).reshape(6,2), columns=list('ab')) % 3 - 1.2

     a    b
0 -1.2 -0.2
1  0.8 -1.2
2 -0.2  0.8
3 -1.2 -0.2
4  0.8 -1.2
5 -0.2  0.8

What's the best way to get the values closest to zero? The expected output of the above would be

     x
0 -0.2
1  0.8 
2 -0.2  
3 -0.2
4  0.8 
5 -0.2 

I tried using df.idxmin(axis=1), and then lookup, but I'm betting there's an easier way?

Upvotes: 2

Views: 5514

Answers (3)

Citizen Kane
Citizen Kane

Reputation: 21

The following worked for me, giving you the value closest to zero:

   df.abs().min()

Upvotes: 2

Stop harming Monica
Stop harming Monica

Reputation: 12590

Just use where to choose between the value in 'a' or 'b'

df['a'].where(df['a'].abs() < df['b'].abs(), df['b'])

My original answer

You can add a column with the column name of the value closest to zero, then use it to select the closest value.

(df.assign(closest=df.apply(lambda x: x.abs().argmin(), axis='columns'))
 .apply(lambda x: x[x['closest']], axis='columns'))

Still it looks more cumbersome than it should be?

Upvotes: 4

Maximilian
Maximilian

Reputation: 8480

One option:

df = pd.DataFrame(pd.np.arange(12).reshape(6,2), columns=list('ab')) % 3 - 1.2
closest_to_zero = df.abs().idxmin(axis=1).pipe(lambda x: x[x.notnull()])
pd.Series(df.lookup(closest_to_zero.index, closest_to_zero), index=closest_to_zero.index).reindex(df.index)

Upvotes: 0

Related Questions