我正在运行此功能5次,希望我的数据帧有5行。但是,它只保存最后一行。任何人都能看到问题吗?任何帮助表示赞赏。谢谢!!
(是的,这很粗糙,但此时我尝试了太多东西)
def Bucket(num1, num2, num3, num4, num5, num6, num7, num8, name, name_total, name_unprocessed, insideframe, counter):
for num1 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=a")]' %name):
print('%s a: ' %name + num1.text)
name_unprocessed = num1.text
for num2 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=b")]' %name):
print('%s b: ' %name + num2.text)
for num3 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=c")]' %name):
print('%s c: '%name + num3.text)
for num4 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=d")]' %name):
print('%s d: ' %name + num4.text)
for num5 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=e")]' %name):
print('%s e: ' %name + num5.text)
for num6 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=f")]' %name):
print('%s f: ' %name + num6.text)
for num7 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=g")]' %name):
print('%s g: ' %name + num7.text)
for num8 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=Duplicate")]' %name):
print('%s h: ' %name + num8.text)
name_total = int(num1.text)+int(num2.text)+int(num3.text)+int(num4.text)+int(num5.text)+int(num6.text)+int(num7.text)+int(num8.text)
print('%s Total: ' %name + str(name_total))
DF2 = {'a': ['%s' % Time],
'b': ['%s' % Date],
'c': ['%s' % Day],
'd': ['%s' % int(num1.text)],
'e': ['%s' % int(num2.text)],
'f': ['%s' % int(num3.text)],
'g': ['%s' % int(num4.text)],
'h': ['%s' % int(num5.text)],
'i': ['%s' % int(num6.text)],
'j': ['%s' % int(num7.text)],
'k': ['%s' % int(num8.text)],
'Total': ['%s' % name_total],
'Bucket': ['%s' % name]}
frame2 = DataFrame(DF2)
if counter == 0:
SMSdf = insideframe.append(frame2)
SMS.df = DataFrame(SMSdf, columns=['Time', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
SMS.df.to_excel('Sprint_Log.xlsx', 'Sheet 1', index=False)
else:
SMSdf = SMSdf.append(frame2)
SMS.df = DataFrame(SMSdf, columns=['Time', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
SMS.df.to_excel('Log.xlsx', 'Sheet 1', index=False)
counter=+1
Bucket(...1)
Bucket(...2)
Bucket(...3)
Bucket(...4)
Bucket(...5)
答案 0 :(得分:0)
每次致电
SMS.df.to_excel('Log.xlsx', 'Sheet 1', index=False)
如果已存在,则覆盖Log.xlsx
。这就是为什么只保存最后一行的原因。
如果传递现有的ExcelWriter对象,则表单将被添加到 现有的工作手册。这可用于将不同的DataFrame保存为一个 工作簿:
>>> writer = ExcelWriter('output.xlsx')
>>> df1.to_excel(writer,'Sheet1')
>>> df2.to_excel(writer,'Sheet2')
>>> writer.save()
因此,您应该在Bucket
之外定义作者一次,将作者传递给Bucket
,然后使用SMS.df.to_excel(writer, sheet, index=False)
。
请注意,每次调用Bucket
时,您还需要写入不同的工作表。
import pandas as pd
def Bucket(num1, num2, num3, num4, num5, num6, num7, num8, name, name_total, name_unprocessed, insideframe, counter, writer):
...
else:
SMSdf = SMSdf.append(frame2)
SMS.df = pd.DataFrame(SMSdf, columns=['Time', 'Date', 'Day', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'Total', 'Bucket'])
sheet = 'Sheet {}'.format(counter)
SMS.df.to_excel(writer, sheet, index=False)
writer = pd.ExcelWriter('output.xlsx')
for counter in range(1, 6):
Bucket(..., counter, writer)
writer.save()
这是一个简单的可运行示例,它将两个数据帧保存到一个xlsx
文件中,然后将其读回:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(np.random.randint(10, size=(5,4)))
df2 = pd.DataFrame(np.random.randint(10, size=(3,6)))
writer = pd.ExcelWriter('/tmp/output.xlsx')
df1.to_excel(writer,'Sheet1')
df2.to_excel(writer,'Sheet2')
writer.save()
reader = pd.ExcelFile('/tmp/output.xlsx')
for sheet in reader.sheet_names:
df = reader.parse(sheet)
print(df)
答案 1 :(得分:0)
这就是我如何将它保存到同一个Excel工作表中的相同数据框中。
def Bucket(num1, num2, num3, num4, num5, num6, num7, num8, name, name_total, name_unprocessed, insideframe):
for num1 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=a")]' %name):
print('%s a: ' %name + num1.text)
name_unprocessed = num1.text
for num2 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=b")]' %name):
print('%s b: ' %name + num2.text)
for num3 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=c")]' %name):
print('%s c: '%name + num3.text)
for num4 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=d")]' %name):
print('%s d: ' %name + num4.text)
for num5 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=e")]' %name):
print('%s e: ' %name + num5.text)
for num6 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=f")]' %name):
print('%s f: ' %name + num6.text)
for num7 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=g")]' %name):
print('%s g: ' %name + num7.text)
for num8 in driver.find_elements_by_xpath('.//a [contains(@href, "%s&singleTopic=1&dashboardSort=Duplicate")]' %name):
print('%s h: ' %name + num8.text)
name_total = int(num1.text)+int(num2.text)+int(num3.text)+int(num4.text)+int(num5.text)+int(num6.text)+int(num7.text)+int(num8.text)
print('%s Total: ' %name + str(name_total))
DF2 = {'a': ['%s' % Time],
'b': ['%s' % Date],
'c': ['%s' % Day],
'd': ['%s' % int(num1.text)],
'e': ['%s' % int(num2.text)],
'f': ['%s' % int(num3.text)],
'g': ['%s' % int(num4.text)],
'h': ['%s' % int(num5.text)],
'i': ['%s' % int(num6.text)],
'j': ['%s' % int(num7.text)],
'k': ['%s' % int(num8.text)],
'Total': ['%s' % name_total],
'Bucket': ['%s' % name]}
Bucket.frame2 = DataFrame(DF2)
Bucket(...1)
SMSdf = insideframe.append(Bucket.frame2)
Bucket(...2)
SMSdf = SMSdf.append(Bucket.frame2)
Bucket(...3)
SMSdf = SMSdf.append(Bucket.frame2)
Bucket(...4)
SMSdf = SMSdf.append(Bucket.frame2)
Bucket(...5)
SMSdf = SMSdf.append(Bucket.frame2)