获取具有特定值的单元格在Pandas中的行和列 [英] Get row and column in Pandas for a cell with a certain value
问题描述
我正在尝试读取未使用Pandas格式化的Excel电子表格.一张纸中有多个表,我想将这些表转换为数据框.由于尚未以传统方式对其进行索引编制",因此没有有意义的列或行索引.有没有一种方法可以搜索特定值并获取行,列所在的位置?例如,假设我要获取包含字符串"Title"的所有单元格的行,列号.
I am trying to read an Excel spreadsheet that is unformatted using Pandas. There are multiple tables within a single sheet and I want to convert these tables into dataframes. Since it is not already "indexed" in the traditional way, there are no meaningful column or row indices. Is there a way to search for a specific value and get the row, column where that is? For example, say I want to get a row, column number for all cells that contain the string "Title".
我已经尝试过DataFrame.filter之类的东西,但仅在具有行和列索引的情况下才有效.
I have already tried things like DataFrame.filter but that only works if there are row and column indices.
推荐答案
您可以做一些冗长而难以阅读的列表理解:
You can do some long and hard to read list comprehension:
# assume this df and that we are looking for 'abc'
df = pd.DataFrame({'col':['abc', 'def','wert','abc'], 'col2':['asdf', 'abc', 'sdfg', 'def']})
[(df[col][df[col].eq('abc')].index[i], df.columns.get_loc(col)) for col in df.columns for i in range(len(df[col][df[col].eq('abc')].index))]
退出:
[(0, 0), (3, 0), (1, 1)]
我应该注意,这是(索引值,列位置)
I should note that this is (index value, column location)
如果您要查找包含某个特定值的任何字符串,也可以将 .eq()
更改为 str.contains()
:
you can also change .eq()
to str.contains()
if you are looking for any strings that contains a certain value:
[(df[col][df[col].str.contains('ab')].index[i], df.columns.get_loc(col)) for col in df.columns for i in range(len(df[col][df[col].str.contains('ab')].index))]
这篇关于获取具有特定值的单元格在Pandas中的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!