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

标签 python pandas indexing

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 方法,它就可以工作

关于python - 按行和列获取单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40173445/

相关文章:

mysql - 分片和索引

c# - 文本索引算法

python - 来自具有 Python 条件的列表的平均值

python - 如何将图像的 HH 子带传递给 greycomatrix 函数?

python - 在 Pandas 中根据条件添加行

MySQL 显示 "possible_keys"但不使用它

python - 带有 re.search() 的原始字符串?

python - matplotlib Axis 值未排序

python - 重命名多个编号的文件

python - Pandas dataframe多索引选择第一个索引第一个元素