5

I have a pandas Dataframe and I want to find the minimum without zeros and Nans. I was trying to combine from numpy nonzero and nanmin, but it does not work.

Does someone has an idea?

2
  • 1
    Can you add some data sample? Commented Jul 30, 2018 at 12:59
  • 1
    Just filter first and get the min Commented Jul 30, 2018 at 13:02

2 Answers 2

7

If you want the minimum of all df, you can try so:

m = np.nanmin(df.replace(0, np.nan).values)
Sign up to request clarification or add additional context in comments.

Comments

2

Use numpy.where with numpy.nanmin:

df = pd.DataFrame({'B':[4,0,4,5,5,np.nan],
                   'C':[7,8,9,np.nan,2,3],
                   'D':[1,np.nan,5,7,1,0],
                   'E':[5,3,0,9,2,4]})

print (df)
     B    C    D  E
0  4.0  7.0  1.0  5
1  0.0  8.0  NaN  3
2  4.0  9.0  5.0  0
3  5.0  NaN  7.0  9
4  5.0  2.0  1.0  2
5  NaN  3.0  0.0  4

Numpy solution:

arr = df.values
a = np.nanmin(np.where(arr == 0, np.nan, arr))
print (a)
1.0

Pandas solution - NaNs are removed by default:

a = df.mask(df==0).min().min()
print (a)
1.0

Performance - for each row is added one NaN value:

np.random.seed(123)
df = pd.DataFrame(np.random.rand(1000,1000))
np.fill_diagonal(df.values, np.nan)
print (df)

#joe answer
In [399]: %timeit np.nanmin(df.replace(0, np.nan).values)
15.3 ms ± 425 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [400]: %%timeit 
     ...: arr = df.values
     ...: a = np.nanmin(np.where(arr == 0, np.nan, arr))
     ...: 
6.41 ms ± 427 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [401]: %%timeit
     ...: df.mask(df==0).min().min()
     ...: 
23.9 ms ± 727 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.