这是示例数据帧,而a是我的列名。
a b x
0 1 3 a
1 2 4 a
2 1 3 b
3 2 5 b
4 2 4 c
需要以这种方式分隔列的唯一值
required output: '1','2'
下面是我的代码,我越来越像这样
x=x1['id'].unique()
x2=','.join("\'"+str(i)+"\'" for i in x)
for this way of code
i'm getting output some thing like this
output:"'1','2'"
**2nd approach:**
x2=','.join("\'"+x1['id']+"\'"):
if i'm do this i'm getting the count of id has been increasing
我需要将输出传递到像select * from abc这样的sql查询中,出于这个原因,在(x2)中需要输出这样的内容
x2 -->'1','2'
i'm getting
x2--->" '1','2'"
尝试对F字符串使用第一种方法,以使事情变得更容易。
x2 =' ,'.join(f"'{str(i)}'" for i in x)
query = rf"""
SELECT
*
FROM
abc
WHERE
a in ({x2})
"""
如果尝试print(query)
,它会给
SELECT
*
FROM
abc
WHERE
a in ('1' ,'2')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句