pandas 在df ["C"]中说7,但实际上要求7的索引会返回empy列表-为什么? [英] Pandas says 7 in df["C"] but actually asking for the indices of 7 returns empy list - why?

查看:39
本文介绍了 pandas 在df ["C"]中说7,但实际上要求7的索引会返回empy列表-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两千行的数据框.其中一列称为C,很有趣的是知道该列中是否出现数字7.

I have a dataframe with a couple of thousand lines. One column is called C and it is of interest to know whether the number 7 occurs in the column.

我尝试过:print(7 in df["C"])并且返回了True,但是print(df.index[df["C"] == 7].tolist())返回了[].

I tried: print(7 in df["C"]) and it returned True, however print(df.index[df["C"] == 7].tolist()) returns [].

这是怎么回事?我想念in的哪个方面?

What is going on here? What aspect of in am I missing?

推荐答案

由于in检查索引的值,因此它为

Because in check value of indices, it is gotcha:

df = pd.DataFrame({
    'A': ['a','a','a','a','b','b','b','c','d'],
    'C': list(range(10, 19))
})
print (df)
   A   C
0  a  10
1  a  11
2  a  12
3  a  13
4  b  14
5  b  15
6  b  16
7  c  17
8  d  18

print(7 in df["C"])
True

print(10 in df["C"])
False

并且在列C中没有值7,因此返回了空列表:

And in column C is no value 7, so returned empty list:

print(df.index[df["C"] == 7].tolist())

这篇关于 pandas 在df ["C"]中说7,但实际上要求7的索引会返回empy列表-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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