如何使用pandas在列中找到最大值?

时间:2017-12-11 16:02:23

标签: python pandas

这是我的代码。我将尝试读取文件并找到最高温度值(第11列)

import pandas as pd

fn = 'Excel.xlsx'
df = pd.read_excel(fn, header=None)
df1 = df.iloc[:,11:12]
m1 = df.iloc[:,11:12].max()
print (m1)

我得到的不是我想要的

python n0.py 
11    Value
dtype: object

我将展示第11列的样子

0      Value
1      18.71
2      20.86
3      18.37
4       12.3
5       5.54
6       3.96
7       8.33
8      12.23
9      11.02
10      4.18
11      7.62
12     15.06
13        20
14     20.39
15     18.26
16     14.54
17      3.81
18      0.93
19      7.74
20     12.42
21      6.59
22      0.12
23      8.32
24     16.82
25     19.46
26      11.8
27      6.25
28      1.96
29     17.13
       ...  
756    15.59
757     6.82
758     7.48
759    14.99
760    17.61
761    19.48
762     16.8
763     9.65
764     1.63
765     9.26
766    16.25
767    24.85
768    14.08
769     0.11
770     6.56
771    12.75
772    21.94
773    20.57
774    17.97
775    12.73
776     6.16
777     9.12
778    19.03
779    17.39
780    14.06
781     1.76
782     8.25
783    17.41
784    20.61
785    20.73

从这些值中,24.85将是最大值,它位于767位置。我最近没有和熊猫一起工作,但这应该是简单的工作。 我的错误在哪里? 我应该使用idmax还是别的什么?

1 个答案:

答案 0 :(得分:2)

首先使用numeric to_numeric将列转换为errors='coerce'

然后需要12列:

max = pd.to_numeric(df.iloc[:,11], errors='coerce').max()

11需要10,因为python来自0

max = pd.to_numeric(df.iloc[:,10], errors='coerce').max()