在PANDAS中,如何获得已知值的索引? [英] In PANDAS, how to get the index of a known value?

查看:202
本文介绍了在PANDAS中,如何获得已知值的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果列中有已知值,我们如何获得其索引值?例如:

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

但如何获取指数按值?

推荐答案

您的值可能有多个索引映射,返回列表更有意义:

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]

这篇关于在PANDAS中,如何获得已知值的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆