根据iloc和用户输入检索Pandas中的数据

时间:2016-10-05 14:16:22

标签: python sqlite pandas

我有一个小型SQLite DB,其中包含有关电导体的数据。程序打印名称和Pandas ID列表,接受该ID的用户输入,然后打印有关所选导体的所有信息。

我试图弄清楚如何从指定列中选择一个特定项目 - 稍后,我将允许输入,但是现在,我只是在print()中手动指定以简化自己的故障排除。

程序一直运行到组合条件打印行。我怀疑将字符串转换为int使得它比它需要的更难;如果有更好的方法来选择一个条目(例如,只是匹配db中名称的名称),我对此持开放态度。

with lite.connect(db_path) as db:

df = pd.read_sql_query('SELECT * FROM cond', conn)
print('What conductor are you analyzing?')

try:
    print(pd.read_sql_query('SELECT name FROM cond', conn)) # List names and Pandas IDs
    getCond = int(input('\nEnter the ID #: ')) # cast string to int to allow .iloc
    printSel = df.iloc[getCond]
    print()
    print(printSel)
    print(df[(df['Amps']) & (df.iloc == getCond)])

finally:
    if conn:
        conn.close()

编辑:选择项目后抛出的错误是

“TypeError:无法将dtyped [object]数组与[bool]类型的标量进行比较”

我很茫然,因为我认为它正在说&操作员正在与某些东西进行比较,而不是将其作为印刷品使用“This AND That。”。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,'getCond'将是您要选择的行的索引,'Amps'是列。我想你可以这样做来返回'Amps'栏的'getCond'行。

df.loc [getCond,'Amps']