我正在编写一个 python 脚本,其中使用 pymysql 连接到我的 MYSQL 数据库并执行查询。
#sql query to get a list of country codes with country names
query = ("SELECT MCC as 'MCC',Name as 'Name' FROM Enum.Country")
cursor.execute(query)
a = cursor.fetchall()
print(a)
#convert a to dataframe
df = pd.DataFrame(list(a),columns=["MCC","Name"])
print(df)
#get the user input
user_input2 = raw_input("From the list, please enter the mcc of the country you want to create the service for:")
我的数据框如下所示:
MCC Name
0 276 Albania
1 603 Algeria
2 0 All Countries
3 213 Andorra
4 631 Angola
5 722 Argentine Republic
6 283 Armenia
7 505 Australia
8 232 Austria
9 400 Azerbaijan
如何检查 user_input2 是否存在于数据帧 df 中? 提前致谢。
最佳答案
你可以这样检查,
df.MCC.isin([user_input2]).any()
关于用于检查数据框中是否存在用户输入值的 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50906416/