如果使用熊猫列作为条件和用户输入的语句

时间:2020-03-16 15:12:40

标签: python pandas if-statement dataset

我有很多问题要解决: 我想使用用户输入来搜索数据集,并为用户提供与其输入相同的完整行。 我完成的代码是类似的东西,但是我不知道如何解决if语句中的问题,任何建议都会很棒。

import pandas as pd

a = pd.read_csv('data.txt', delimiter = '\t')

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)

a.set_index('Rank', inplace = True)
user_rank = input('Choose the rank: ')

if int(user_rank) == all(a.iloc['Rank']):
  print(a.loc[user_rank])
else:
  print('Error')

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案是:

import pandas as pd

a = pd.read_csv('data.txt', delimiter = '\t')

pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)

a.set_index('Rank', inplace = True)
user_rank = input('Choose the rank: ')

if int(user_rank) == all(a.loc['Rank']):
  print(a.loc[user_rank])
else:
  print('Error')