先说效果:
如图,本来是这样的一个DF表,表示各字段限制条件(A、B、C、D均为字段名)
通过下面步骤转化为一条sql语句,当然也做了反向操作
for i in range(3):
locals()['f'+str(i)]=[]
import pandas as pd
import numpy as np
a = pd.DataFrame({"A":[">=",5],
"B":["<",6],
"C":["in",'("ha","he")'],
"D":["like","*q*"]
})
print(a)
b = []
for i in a.columns:
b.append(str(i)+" "+str(a[i][0])+" "+str(a[i][1]).strip())
c = " and ".join(b)
#b = b+str(i)+" "+str(a[i][0])+" "+str(a[i][1])+" "+"and"+" "
#print(c)
d = c.split(" and ")
for i in range(3):
locals()['f'+str(i)]=[]
for j in (range(len(d))):
f = d[j].split(" ")
locals()['f' + str(i)].append(f[i])
print(f0,f1,f2)
g = pd.DataFrame(columns=f0,data=[f1,f2])
print(g)