在Pandas数据框中定位特定单元格的最快方法是什么?

huangapple go评论140阅读模式
英文:

fastest way to locate specific cell in pandas dataframe?

问题

我想知道如何轻松访问pandas中特定单元格的列索引。

例如,如果我有这个数据框:

df = pd.DataFrame.from_dict({0: ['01_CF56_1'], 1: ['05_CF41_3'], 2: ['06_CF44_2']})

我想要一个函数,给我

f('05_CF41_3') => 1

因为这个值位于列索引1(我不关心行)

当然,幼稚的方法是遍历所有行和列,匹配单元格内容,但我想知道是否有更优雅的解决方案。

英文:

I wondered how to easy access the column index for a specific cell in pandas.

E.g. if i got this dataframe:

df = pd.DataFrame.from_dict({0:['01_CF56_1'],	1:['05_CF41_3'], 2:['06_CF44_2']})

i'd like to have a function that gives me

f('05_CF41_3') => 1

because this value is in the column index 1 (I don't care about the row)

of course the naive way would be to go through all rows and columns and match the cell contents, but i wonder if there is a more elegant solution

答案1

得分: 0

你可以使用indices的第二个元素来获取列索引,并在 where 中使用它。然后,使用 argmax 找到第一个有效的列索引:

np.argmax(np.where(df == '05_CF41_3', np.indices(df.shape)[1], -1) >= 0)

返回 1

如果要搜索的值可能出现在多列中,你可以使用以下方法获取所有包含该值的列编号的列表:

a = np.where(df == '05_CF41_3', np.indices(df.shape)[1], -1)
list(set(a[a >= 0]))
英文:

You could use the second element of indices to get the column indices and use it in where. Then find the first valid column index using argmax:

np.argmax(np.where(df == '05_CF41_3', np.indices(df.shape)[1], -1) >= 0)

returns 1.

If the searched for value may occur in multiple columns you could use something like that to get a list of all column numbers where this value occurs:

a = np.where(df == '05_CF41_3', np.indices(df.shape)[1], -1)
list(set(a[a>=0]))

huangapple
  • 本文由 发表于 2023年6月29日 01:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定