首页 > 解决方案 > 如何在数据框中打印您要查找的值的行和列

问题描述

所以我做了这个数据框

alp = "abcdefghijklmnopqrstuvwxyz0123456789"
s = "carl"

for i in s: 
    alp = alp.replace(i,"")
    
jaa = s+alp
x = list(jaa)


array = np.array(x)
re = np.reshape(array,(6,6))

dt = pd.DataFrame(re)
dt.columns = [1,2,3,4,5,6]
dt.index = [1,2,3,4,5,6]
dt

    1   2   3   4   5   6
1   c   a   r   l   b   d
2   e   f   g   h   i   j
3   k   m   n   o   p   q
4   s   t   u   v   w   x
5   y   z   0   1   2   3
6   4   5   6   7   8   9

我想搜索一个值,并打印它的行(索引)和列。例如'h',我想要的输出是2,4。有没有办法得到那个输出?

标签: pythonpandasdataframe

解决方案


row, col = np.where(dt == "h")
print(dt.index[row[0]], dt.columns[col[0]])

推荐阅读