熊猫-检查值是否在列中,如果不要求输入

时间:2018-07-25 09:58:49

标签: python pandas

我确定此问题已在堆栈溢出时得到了解答,但似乎找不到任何东西。

我有一个包含3列的数据框:“爱好”,“名字”,“姓氏”。在记事本中打开时看起来像这样:

,hobby,first_name,last_name
0,football,james,smith
1,tennis,jill,murray

(第一列是索引) 我想问用户一个爱好的输入,如果不在爱好列中,则需要重新输入。我有这段代码,并对为什么它不起作用感到非常困惑:

hobby = input('enter a hobby')
while hobby not in df['hobby']:
    hobby = input('enter a hobby')

事实证明这是一个无限循环,即使我首先输入列中的兴趣爱好也不起作用

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

对于重复输入,始终建议使用while循环;

while True:             # Loop continuously
    inp = raw_input()   # Get the input
    if (df['hobby'] == hobby).any():      # If it is in the column..
        break