我有一个数据集,其中我有不同的组,看起来像这样:
A B
Base1 100
MemberA Base1+1
MemberB Base1+2
Base2 250
MemberC Base2+1
MemberD Base2+2
etc.
我用pandas(df(创建了新的列,其中包含组名和要添加到基的值,还显示了单元格中是否有"+"(col.B((位置((-1(。我还把它保存为一个excel文件,我用pyodbc打开了它。
A B c D E
Base1 100 Nan Nan -1
MemberA Base1+1 Base1 1 5
MemberB Base1+2 Base1 2 5
Base2 250 Nan Nan -1
MemberC Base2+1 Base2 1 5
MemberD Base2+2 Base2 2 5
etc.
我想创建另一个列(F(,其中基值被添加到第D列中的数字中,然后将其保存为excel文件。
A B c D E F
Base1 100 Nan Nan -1 Nan
MemberA Base1+1 Base1 1 5 101
MemberB Base1+2 Base1 2 5 102
Base2 250 Nan Nan -1 Nan
MemberC Base2+1 Base2 1 5 251
MemberD Base2+2 Base2 2 5 252
etc.
我试着用pyodbc这样解决它:
crsr.exeute("SELECT C FROM Table WHERE E > 0")
for row in crsr.fethcall():
crsr.execute("SELECT B FROM Table WHERE A=?", row)
while True:
row=crsr.fetchone()
if row==None:
break
else:
initial= row
value = initial + D
但它只给最后一组的基本值赋予D值。谢谢你的帮助!
同时我找到了一个解决方案。不是最好的,但它对我有效。
listA=[]
crsr.exeute("SELECT C FROM Table WHERE E > 0")
for row in crsr.fethcall():
crsr.execute("SELECT B FROM Table WHERE A=?", row)
while True:
row2=crsr.fetchone()
if row==None:
break
else:
row2=pd.to_numeric(row2)
crsr.execute("SELECT D FROM Table WHERE C=?",row)
while True:
row3=crsr.fetchone()
if row3==None:
break
else:
listA.extend(row2)
row3=pd.to_numeric(row3)
value= row3 + row2
listA.extend(value)
listA=list(dict.fromkeys(listA))
Table["F"]=listA
我得到的结果是:
A B c D E F
Base1 100 Nan Nan -1 100
MemberA Base1+1 Base1 1 5 101
MemberB Base1+2 Base1 2 5 102
Base2 250 Nan Nan -1 250
MemberC Base2+1 Base2 1 5 251
MemberD Base2+2 Base2 2 5 252
etc.