我创建了一个for循环以遍历数据帧并将其导出到csv
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard CharacterSet(charactersIn: "0123456789").isSuperset(of: CharacterSet(charactersIn: string)) else {
return false
}
return true
}
csv创建成功,但是由于“ df”位于for循环中,因此foor循环进行了多次,也创建了Price / Location / Areasize列,
我试图像这样在foor循环之外删除df:
for x in range (len(price)):
df = pd.DataFrame(data1, columns= ['Price', 'Location', 'Area size'])
data1 = {'Price': [price[x].text],
'Location': [location[x].text],
'Size': [area_size[x].text]}
df.to_csv('static/home.csv', header="False", mode="a")
driver.quit()
但是它给出一个错误,即“未定义data1”。知道如何通过不遍历Price / Location / Area列而不创建多次的方式来创建for循环吗?
答案 0 :(得分:0)
您应该使用df.append()
df = pd.DataFrame(data1, columns= ['Price', 'Location', 'Area size'])
for x in range (len(price)):
# data1 is dataframe , i dont #know how your data looks like, so #you need to prepare it on your own #before append
df=df.append(data1)
df.to_csv(“path”)