arcee123
arcee123

Reputation: 243

In a Pandas dataframe why does the max() function skip columns?

I have a pandas dataframe. The head() of this is below:

print(df.head())

       0     1       2  3   4   5  6     7       8    9      10      11  \
2  4077.7  0.21  313.42  N  46  35  w     0  4077.6  9.1  235.3  5.18 S   
4    4100  0.16  320.55  N  39  27  W  0.26  4099.9  9.1  235.8  5.13 S   
5    4150   0.1  295.47  N  64  32  W  0.16  4149.9  9.1  236.4  5.06 S   
6    4200  0.07  353.59  N   6  24  W  0.17  4199.9  9.2  236.8  5.01 S   
7    4250   0.1   13.67  N  13  40  E  0.08  4249.9  9.1  237.2  4.94 S   

       12  
2  7.49 W  
4   7.54W  
5  7.62 W  
6   7.66W  
7   7.66W  

print(df.max())

0       11150
1        2.26
3           s
5          59
6           w
7        7.69
8     11149.7
9        14.9
10      257.4
11     5.57 S
12     9.88 W
dtype: object

why would a max() function skip column 2? How do I troubleshoot? I need to ensure that the max() and min() functions include all columns.

Thank you very much.

Upvotes: 0

Views: 389

Answers (1)

DSM
DSM

Reputation: 353179

This is typically a sign you have a mixed type in a column:

In [26]: df = pd.DataFrame([[1,2,3],[4,'5',6],[7,8,9]], columns=['a','b', 'c'])

In [27]: df
Out[27]: 
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

In [28]: df.max()
Out[28]: 
a    7
c    9
dtype: int64

In [29]: df.dtypes
Out[29]: 
a     int64
b    object
c     int64
dtype: object

If you force numeric_only=False, you'll probably get an error like this:

In [31]: df.max(numeric_only=False)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[...]
TypeError: '>=' not supported between instances of 'int' and 'str'

Look at the types of the values in your df[2] to see what's going on.

Upvotes: 3

Related Questions