我有如下所示的数据框
val
5
7
17
95
23
45
df = pd.read_clipboard()
我想获取每个值的 excel 单元格/索引/行号
我正在尝试类似下面的东西,但不确定我是否过于复杂
for row in range(1,99999):
for col in range(1,2999999):
if wb.sheets[1].range((row,col)).value == 5:
print("The Row is: "+str(row)+" and the column is "+str(col))
else
row = row + 1
我也尝试过类似下面的方法,但也不起作用
wb.sheets[1].range("A9:B500").options(pd.DataFrame).value
基本上,我有一个值列表,并想找出它们出现在 excel 中的哪一列和哪一行。有人能帮助我吗?
我希望我的输出如下所示。Excel 行号是从 excel 中提取/找到的
请记住,熊猫数据帧的索引是从零开始的,而 Excel 工作表的行号是从一开始的,因此,如果您想用熊猫解决这个问题
df["index"] = df.index.astype(int) + 1
是正确的。但是,请注意,您问题中的图片显示了 Excel 工作表的一部分,该工作表应用了一个活动过滤器。此处提供的解决方案仅适用于未过滤 excel 中的数据的情况。
您还可以在.isin中指定值并使用以下代码(不漂亮)根据索引和列获取它们的位置:
import pandas as pd
df = pd.DataFrame((
(2, 5, 8),
(20, 5, 772),
(0, 20, 5),
(2, 0, 0),
(76, 35, 66),
(23, 21, 29),
(5, 55, 88),
(44, 23, 5),
(1, 2, 3),
), columns=["a", "b", "c"])
cols = df.columns.to_list()
for col in cols:
filt = df[col].isin([5, 2])
df_x = df.loc[filt]
for row, vals in df_x.iteritems():
for val in vals.index:
print(f"index:{val:4} column: {col}")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句