Skip to main content
 首页 » 编程设计

python之按行和列获取单元格的值

2024年10月17日8unruledboy

CD_FARE MTH DAY ID_CALENDAR   H0    H1  H2  H3  PE1   PE2    PE3    PE4 
2.0      1   M    Cal01       1     2   1   3   0.14  0.15   0.1    0.2  
2.0      1   T    Cal01       1     2   1   3   0.14  0.16   0.1    0.2 
2.0      1   W    Cal01       1     2   4   3   0.14  0.12   0.1    0.2 
2.0      1   T    Cal01       2     2   1   3   0.14  0.11*   0.1    0.2 
2.0      1   F    Cal01       4     2   1   3   0.14  0.18   0.1    0.2  

我想知道如何从特定单元格中获取值。

例如:我想返回值 0.11。 我知道行的位置(在本例中为 3)和列的名称 (PE2)。 我可以这样选择数据吗?:

data = df.iloc[3, 'PE2'] 

请您参考如下方法:

显然它不起作用,它给出了一个 ValueError

ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types 

但是如果你使用 df.loc[3, 'PE2'] 而不是 iloc 方法,它就可以工作