mysql - pandas sql使用包含列表的var
问题描述
我通过这样做创建了一个位置列表:
list_NA = []
for x in df['place']:
if x and x not in list_NA:
list_NA.append(x)
这给了我一个这样的列表:
print(list_NA)
['DEN', 'BOS', 'DAB', 'MIB', 'SAA', 'LAB', 'NYB', 'AGA', 'QRO', 'DCC', 'PBC', 'MIC', 'MDW', 'SAB', 'LAA', 'NYA', 'PHL', 'DCB', 'CHA', 'CHB', 'SEB', 'AGB', 'SEC', 'DAA', 'MEX']
我想在我的 where 子句中使用这个列表,如下所示:
df2 = pd.read_sql("select airport from "+db+" where airport in "+list_NA+"", conn)
但我不断收到此错误:
TypeError: Can't convert 'list' object to str implicitly
我试图做 str(list_NA) 或 tuple(list_NA) 但
解决方案
您需要将 list_NA 转换为带单引号的逗号分隔字符串。
"','".join(list_NA)
但您还需要在两端用单引号括起来。
df2 = pd.read_sql("select airport from "+db+" where airport in ('"+ "','".join(list_NA) +"')", conn)
推荐阅读
- r - 使用带有外部参数的 group_by 和 slice
- python - 如何在python二进制搜索中获取树的长度
- react-native - 当 useEffect 中没有 fetch 时,取消 axios 请求
- c# - 带有 put 和 post 端点的 Swagger UI 导致 UI 崩溃
- asp.net - 如何阻止 Ionic zip 将 null 附加到文件末尾
- linux - -bash: ./ideal.exe: 没有这样的文件或目录
- node.js - SyntaxError: await 仅在 nodeJs 的异步函数中有效
- excel - Solver VBA隐藏对话框,仍然出现
- python - 为自定义分布式训练覆盖 Apply_gradients
- java - Java:根据另一个数组的索引顺序对数组进行排序