Python 3.x 我如何检查输入是否确实在pandas中的列中?

Python 3.x 我如何检查输入是否确实在pandas中的列中?,python-3.x,pandas,Python 3.x,Pandas,数据帧的示例: df = {'column': 'hello'} 输入应该是数据框中的一个条目 if 'hello' in df['column']: print("hello") else: print("Couldn't find entry) 我想要的输出是: Hello 但是,输出结果如下所示: Couldn't find entry 我已经检查过了,输入中输入的值肯定在dataFrame的该列中 我不知道我是否做得不正确,也不知道为什么它不起作用试试这个,并评论它是否

数据帧的示例:

df = {'column': 'hello'}
输入应该是数据框中的一个条目

if 'hello' in df['column']:
   print("hello")
else:
  print("Couldn't find entry)
我想要的输出是:

Hello
但是,输出结果如下所示:

Couldn't find entry
我已经检查过了,输入中输入的值肯定在dataFrame的该列中
我不知道我是否做得不正确,也不知道为什么它不起作用

试试这个,并评论它是否给出了正确的输出

df = {'column': 'hello'}
if 'hello' == df['column']:
   print("hello")
else:
  print("Couldn't find entry")

尝试此操作并评论它是否提供了正确的输出

df = {'column': 'hello'}
if 'hello' == df['column']:
   print("hello")
else:
  print("Couldn't find entry")

我运行了您的代码并获得了正确的输出:

df = {'column': 'hello'}

if 'hello' in df['column']:
   print("hello")
else:
  print("Couldn't find entry")
输出:

hello

我运行了您的代码并获得了正确的输出:

df = {'column': 'hello'}

if 'hello' in df['column']:
   print("hello")
else:
  print("Couldn't find entry")
输出:

hello
使用df.values:

for i in df.values:
 if 'hello' in i:
    print("hello")
 else:
    print("Couldn't find entry")
使用df.values:

for i in df.values:
 if 'hello' in i:
    print("hello")
 else:
    print("Couldn't find entry")

我想如果您将df['column']中的
if'hello':
替换为df['column']中的
if'hello'。tolist():
,可能会有用。@Sajan非常感谢!它与.tolist()一起工作。你知道为什么它不能像我写的那样工作吗?
tolist()
函数会将数据帧列的值转换成一个
列表
,然后就可以循环使用它了。我尝试了你的代码,但它给出了一个错误。不确定,但可能无法这样访问数据帧值。我认为如果将df['column']中的'hello'替换为df['column']中的'hello'。tolist():,可能会有用。@Sajan非常感谢!它与.tolist()一起工作。你知道为什么它不能像我写的那样工作吗?
tolist()
函数会将数据帧列的值转换成一个
列表
,然后就可以循环使用它了。我尝试了你的代码,但它给出了一个错误。不确定,但可能,数据帧值不能像那样访问。谢谢,我想写我的问题作为你的第一个例子。我现在已经编辑了它,但是我的问题仍然是相同的。你知道为什么它在比较(“==”)时有效,而在中使用时无效吗?谢谢,我想写我的问题作为你的第一个例子。我现在已经编辑了它,但是我的问题仍然是相同的。你知道为什么它在比较(“==”)时有效,而在中使用时无效吗?