使用pandas python从sheet1的数据中将sheet2添加到现有的excelfile中

我正在从网上抓取数据到Excel工作表使用pandas&能够将其保存到工作表1,现在我想要获取列数据到工作表2相同的Excel中。

当我执行代码时,它仍然不会在excelfile中创build新工作表,只是用新名称和所需数据覆盖现有工作表。

我已经创build了两个函数,第一个函数创build所需的数据和函数2的Excel文件来获取列值和创build该列值的新工作表

这是function2

def excelUpdate(): xls_file = pd.ExcelFile('Abc.xlsx') df = xls_file.parse(0) data=[] for i in df.index: x=df['Category'][i] print(df['Category'][i]) data.append(x) table1 = pd.DataFrame(data) table1.to_excel(writer, sheet_name='Categories') writer.save() 

另外我想要得到表2中特定类别的计数。请帮助

样本数据

我已经在表2中突出显示了我想要的数据,我希望表2中每个类别的分类名称的数量

 Index | AppVersion | Author | **Category** | Description | Rating | Text 0 | 1.15 | Miuwu | **Slow** | Worthless | 1 | Worked fine while I was home, a week later and 3000 miles away nothing!! 1 | 1.15 | abc | **Problem** | Self-reboot | 1 | No such option. 2 | 1.15 | Rax | **Design** | Self-reboot | 1 | No such option. 3 | 1.15 | golo7 | **Problem** | Self-reboot | 1 | No such option. 4 | 1.15 | Marcog | **Problem** | Self-reboot | 1 | No such option. 

您可以使用openpyxlpandas库使用的openpyxl来实现这一点:

 import pandas as pd from openpyxl import load_workbook book = load_workbook('Abc.xlsx') writer = pd.ExcelWriter('Abc.xlsx', engine='openpyxl') writer.book = book writer.sheets = dict((ws.title, ws) for ws in book.worksheets) 

然后,准备好你的table1

 df['Category'].value_counts().to_frame().to_excel(writer, sheet_name='Categories') writer.save()