Python Pandas-查找数据帧中任意位置的值索引
我是Python&Pandas的新手 我想在pandas数据帧中找到某个值的索引(比如说Python Pandas-查找数据帧中任意位置的值索引,python,pandas,Python,Pandas,我是Python&Pandas的新手 我想在pandas数据帧中找到某个值的索引(比如说security\u id),因为这是列的起始位置。 (列上方的行数未知,数据不相关,左侧的“列”为空。) 在我看来,该方法只返回一个布尔值,而不是它的索引 如何找到该值的索引?我想以前可能有人问过这个问题。公认的答案相当全面,应该可以帮助您在列中找到值的索引 编辑: 如果值所在的列未知,则可以使用: for col in df.columns: df[df[col] == 'security_id'
security\u id
),因为这是列的起始位置。
(列上方的行数未知,数据不相关,左侧的“列”为空。)
在我看来,该方法只返回一个布尔值,而不是它的索引
如何找到该值的索引?我想以前可能有人问过这个问题。公认的答案相当全面,应该可以帮助您在列中找到值的索引 编辑: 如果值所在的列未知,则可以使用:
for col in df.columns:
df[df[col] == 'security_id'].index.tolist()
假设您的数据帧如下所示:
0 1 2 3 4
0 a er tfr sdf 34
1 rt tyh fgd thy rer
2 1 2 3 4 5
3 6 7 8 9 10
4 dsf wew security_id name age
5 dfs bgbf 121 jason 34
6 dddp gpot 5754 mike 37
7 fpoo werwrw 342 jack 31
for row in range(df.shape[0]): # df is the DataFrame
for col in range(df.shape[1]):
if df.get_value(row,col) == 'security_id':
print(row, col)
break
请执行以下操作:
0 1 2 3 4
0 a er tfr sdf 34
1 rt tyh fgd thy rer
2 1 2 3 4 5
3 6 7 8 9 10
4 dsf wew security_id name age
5 dfs bgbf 121 jason 34
6 dddp gpot 5754 mike 37
7 fpoo werwrw 342 jack 31
for row in range(df.shape[0]): # df is the DataFrame
for col in range(df.shape[1]):
if df.get_value(row,col) == 'security_id':
print(row, col)
break
您要查找的值不重复:
poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all')
value=poz.iloc[0,0]
index=poz.index.item()
column=poz.columns.item()
matrix=pd.DataFrame([[1,1],[1,np.NAN]],index=['q','g'],columns=['f','h'])
matrix
Out[83]:
f h
q 1 1.0
g 1 NaN
poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all')
index=poz.stack().index.tolist()
index
Out[87]: [('q', 'f'), ('q', 'h'), ('g', 'f')]
您可以获取它的索引和列
重复:
poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all')
value=poz.iloc[0,0]
index=poz.index.item()
column=poz.columns.item()
matrix=pd.DataFrame([[1,1],[1,np.NAN]],index=['q','g'],columns=['f','h'])
matrix
Out[83]:
f h
q 1 1.0
g 1 NaN
poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all')
index=poz.stack().index.tolist()
index
Out[87]: [('q', 'f'), ('q', 'h'), ('g', 'f')]
您将得到一个避免显式循环的单行解决方案列表
- 返回整行 df.iloc[np.flatnonzero((df=='security_id').values)//df.shape[1],:]
- 返回行和列 df.iloc[ np.flatnonzero((df=='security_id').values)//df.shape[1], np.unique(np.flatnonzero((df=='security\u id').values)%df.shape[1]) ]
search = 'security_id'
df.loc[df.isin([search]).any(axis=1)].index.tolist()
search = 'search term'
df.loc[df.isin([search]).any(axis=1)]
筛选所有列中匹配搜索词的行
search = 'security_id'
df.loc[df.isin([search]).any(axis=1)].index.tolist()
search = 'search term'
df.loc[df.isin([search]).any(axis=1)]
函数查找值在数据帧中的位置
import pandas as pd
import numpy as np
def pandasFindPositionsInDataframe(dfIn,findme):
positions = []
irow =0
while ( irow < len(dfIn.index)):
list_colPositions=dfIn.columns[dfIn.iloc[irow,:]==findme].tolist()
if list_colPositions != []:
colu_iloc = dfIn.columns.get_loc(list_colPositions[0])
positions.append([irow, colu_iloc])
irow +=1
return positions
将熊猫作为pd导入
将numpy作为np导入
def pandasFindPositionsInDataframe(dfIn,findme):
职位=[]
irow=0
而(irow
欢迎来到StackOverflow。请花点时间阅读这篇文章,以及如何提供答案,并相应地修改你的问题。这些提示也可能有用。在给定的问题上,该专栏是已知的。在我的例子中,我不知道该值出现在哪一列。但我同意它为我的问题的答案指明了方向啊,抱歉!您可以在数据框中的列上循环,并应用上面链接的答案<对于df.columns中的col,代码>为df[df[col]=='security\u id'].index.tolist()。这也会为您提供所需内容的所有实例。谢谢,这似乎是一个解决方案:)但这是找到迭代行和列的值的唯一方法吗?会有更有效的方法吗?无论你做什么,迭代都会涉及到。要么你去做,要么熊猫去做。内部迭代总是会涉及到的。此外,迭代一旦停止,您就会得到ID。最糟糕的情况是,security_ID是数据帧(O(mn))的右下角元素。如果security_id位于数据帧的左上半部分,则成本不会太高。此外,您要求进行数据清理。因此,这是一个廉价的预处理步骤。不要试图超优化一切。过早优化是万恶之源。记住。是的,这是有道理的,我想可能是这样(在任何情况下都是迭代)。感谢您的解释。这可能是最有效的答案,因为它使用了.loc
。回答得好!