pandas 在PANDAS中,如何获取已知值的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16683701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In PANDAS, how to get the index of a known value?
提问by user2407991
If we have a known value in a column, how can we get its index-value? For example:
如果我们在列中有一个已知值,我们如何获得它的索引值?例如:
In [148]: a = pd.DataFrame(np.arange(10).reshape(5,2),columns=['c1','c2'])
In [149]: a
Out[149]:
c1 c2
0 0 1
1 2 3
2 4 5
........
As we know, we can get a value by the index corresponding to it, like this.
众所周知,我们可以通过它对应的索引来获取一个值,就像这样。
In [151]: a.ix[0,1] In [152]: a.c2[0] In [154]: a.c2.ix[0] <-- use index
Out[151]: 1 Out[152]: 1 Out[154]: 1 <-- get value
But how to get the index by value?
但是如何按值获取索引呢?
回答by waitingkuo
There might be more than one index map to your value, it make more sense to return a list:
可能有多个索引映射到您的值,返回一个列表更有意义:
In [48]: a
Out[48]:
c1 c2
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
In [49]: a.c1[a.c1 == 8].index.tolist()
Out[49]: [4]
回答by gxpr
Using the .loc[] accessor:
使用 .loc[] 访问器:
In [25]: a.loc[a['c1'] == 8].index[0]
Out[25]: 4
Can also use the get_loc() by setting 'c1' as the index. This will not change the original dataframe.
也可以通过将“c1”设置为索引来使用 get_loc()。这不会改变原始数据帧。
In [17]: a.set_index('c1').index.get_loc(8)
Out[17]: 4
回答by Surya
The other way around using numpy.where() :
使用 numpy.where() 的另一种方式:
import numpy as np
import pandas as pd
In [800]: df = pd.DataFrame(np.arange(10).reshape(5,2),columns=['c1','c2'])
In [801]: df
Out[801]:
c1 c2
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
In [802]: np.where(df["c1"]==6)
Out[802]: (array([3]),)
In [803]: indices = list(np.where(df["c1"]==6)[0])
In [804]: df.iloc[indices]
Out[804]:
c1 c2
3 6 7
In [805]: df.iloc[indices].index
Out[805]: Int64Index([3], dtype='int64')
In [806]: df.iloc[indices].index.tolist()
Out[806]: [3]
回答by RumbleFish
To get the index by value, simply add .index[0]to the end of a query. This will return the index of the first row of the result...
要按值获取索引,只需将.index[0]添加 到查询的末尾。这将返回结果第一行的索引...
So, applied to your dataframe:
因此,应用于您的数据框:
In [1]: a[a['c2'] == 1].index[0] In [2]: a[a['c1'] > 7].index[0]
Out[1]: 0 Out[2]: 4
Where the query returns more than one row, the additional index results can be accessed by specifying the desired index, e.g. .index[n]
在查询返回多于一行的情况下,可以通过指定所需的索引来访问额外的索引结果,例如.index[n]
In [3]: a[a['c2'] >= 7].index[1] In [4]: a[(a['c2'] > 1) & (a['c1'] < 8)].index[2]
Out[3]: 4 Out[4]: 3
回答by Jay
I think this may help you , both index and columns of the values.
我认为这可能对您有所帮助,包括值的索引和列。
value you are looking for is not duplicated:
您正在寻找的值不重复:
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()
you can get its index and column
你可以得到它的索引和列
duplicated:
重复:
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')]
you will get a list
你会得到一个清单