简体   繁体   English

检查数据框列值中是否存在用户输入

[英]Check if user input exist in the dataframe column value

Example SQL query: my_query = SELECT a.fruits, a.colors FROM table1示例 SQL 查询: my_query = SELECT a.fruits, a.colors FROM table1

Table

    a.fruits    a.colors

     apple      red

     grapes     purple

     mango      yellow
DF

       fruits    colors

1      apple      red

2      grapes    purple

3      mango     yellow

cursor = conn.cursor()
cursor.execute(my_query)
df = pd.DataFrame(cursor.fetchall(),columns(['fruits','colors']))

user = args.add.lower()
conn.close()

<How can I query the dataframe, to check if the user input exists in the df?> <如何查询数据框,以检查用户输入是否存在于 df 中?>

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a','--add')
args = parser.parse_args()

User input python3 -a grapes用户输入python3 -a grapes

Expected output if it exist: grapes, purple预期输出(如果存在): grapes, purple

Are you looking for something like:您是否正在寻找类似的东西:

import argparse
import pandas as pd
from sqlalchemy import create_engine

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-a','--add', dest='fruit')
args = parser.parse_args()

qs = "SELECT fruits,colors FROM table1 WHERE fruits = :fruit"

engine = create_engine('sqlite:///data.db')
df = pd.read_sql(qs, engine, params=vars(args))
print(df)

Output:输出:

[...]$ python db.py -a grapes
   fruits  colors
0  grapes  purple

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python Dataframe按列值和列名过滤列是用户输入 - python Dataframe filter column by column value and column name is user input 检查每个列值是否存在于另一个 dataframe 列中,其中另一个列值是列 header - Check if each column values exist in another dataframe column where another column value is the column header 如何检查 1 个数据帧中的列中的整数值是否存在于第 2 个数据帧中 2 列之间的范围拆分中? - How do I check for an integer value in a column in 1 dataframe to exist in a range split between 2 columns in 2nd dataframe? 检查 dataframe 中的每一行和每一列,并用用户定义的 function 替换值 - check each row and column in dataframe and replace value with user define function 检查列中的值是否存在于 dataframe 行中的其他位置 - Check if values in a column exist elsewhere in a dataframe row 检查值是否在 Pandas dataframe 列中 - Check if value is in Pandas dataframe column 与 Dataframe 值匹配的用户输入 - User input that matches a Dataframe value 如何检查和计算其他数据帧中是否存在数据帧的值? - How to check and count if value of dataframe one exist in other dataframe? Pandas Dataframe 检查列值是否在列列表中 - Pandas Dataframe Check if column value is in column list 检查列中是否存在值并在另一个 Pandas 中更改 - Check if a value exist in a column and change in another Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM