给出如下内容
>>> breakfast = pd.DataFrame([[2, 2, 3],
[1, 2, 3],
[3, 0, 2]],
index=['Thuy', 'Kana', 'Malcolm'],
columns=['eggs', 'bacon', 'pancakes'])
>>> breakfast
eggs bacon pancakes
Thuy 2 2 3
Kana 1 2 3
Malcolm 3 0 2
>>> breakfast.iloc[1,1]
2
是否还可以看到 breakfast.iloc[1,1] 的 (row, col) 对是 ('Kana', '培根')。这对于我拥有的对角对称的大型数据框来说会很方便。我正在使用 .iloc 仅迭代一半以上,但这样做会丢失行和列信息。
请您参考如下方法:
您可以定义自己的函数来按序号位置返回索引和列标签值:
In[96]:
def row_col(index_pos, column_pos):
return (breakfast.index[index_pos], breakfast.columns[column_pos])
row_col(1,1)
Out[96]: ('Kana', 'bacon')
