python将新建df按两列分组后,分组的两个列名分别为表名和sheet名,如果表名和分组表名相同,批量写入原始excel中倒数第2个新SHEET
时间: 2023-06-12 07:02:22 浏览: 67
回答:
您好,感谢您对ChitGPT的使用。
关于您的问题,根据您的描述,您可以先将原始Excel读入到Python中,然后使用pandas库进行分组操作。具体步骤如下:
1. 使用pandas库读取原始Excel表格:
```python
import pandas as pd
df = pd.read_excel('原始表格.xlsx')
```
2. 按照表名和sheet名进行分组:
```python
grouped = df.groupby(['表名', 'sheet名'])
```
3. 遍历分组后的数据,并将每组数据写入到原始Excel表格的倒数第2个新sheet中:
```python
writer = pd.ExcelWriter('原始表格.xlsx', engine='openpyxl')
book = openpyxl.load_workbook('原始表格.xlsx')
writer.book = book
for name, group in grouped:
table_name, sheet_name = name
if table_name == sheet_name:
group.to_excel(writer, sheet_name='new_sheet', index=False)
```
这样,就可以将分组后的数据写入到原始Excel表格的倒数第2个新sheet中了。
希望这个回答能够帮到您。如果您还有其他问题,可以随时向我提问。
相关问题
用python将新建df按两列分组后,分组的两个列名分别为表名和sheet名,批量写入现有excel中新建倒数第2个SHEET
您好!非常感谢您使用ChitGPT来咨询技术问题。关于您提出的问题,可以使用pandas库进行操作,代码示例如下:
```python
import pandas as pd
import openpyxl
# 读取现有的Excel文件
wb = openpyxl.load_workbook('test.xlsx')
# 新建一个DataFrame数据表
df = pd.DataFrame(data={'表名': ['表1', '表1', '表2', '表2'], 'sheet名': ['sheet1', 'sheet2', 'sheet1', 'sheet2'], '数据': [1, 2, 3, 4]})
# 分组,按照表名和sheet名进行分组
gb = df.groupby(['表名', 'sheet名'])
# 获取分组后的dataframe
for name, group in gb:
# 找到要写入的Sheet
sheet = wb.create_sheet(title=name[1], index=-1)
# 将数据写入Sheet中
for idx, row in group.iterrows():
sheet.append((row['表名'], row['sheet名'], row['数据']))
# 保存Excel文件
wb.save('test.xlsx')
```
以上代码将新建的DataFrame数据表按照表名和sheet名进行分组后,批量写入现有的Excel文件中倒数第2个SHEET。希望能对您有所帮助。如果您还有其他问题,可以随时提出。
用python将新建df按两列分组后,分组的两个列名分别为表名和sheet名,批量写入现在excel中倒数第2个新SHEET
您好!感谢您的提问。根据您的要求,可以通过以下代码实现:
```python
# 导入需要使用的库
import pandas as pd
import openpyxl
# 定义要分组的两个列名
group_cols = ['表名', 'sheet名']
# 读取要写入的数据集
new_df = pd.read_csv('new_data.csv')
# 按照指定列名进行分组
grouped = new_df.groupby(group_cols)
# 获取现有excel文件
workbook = openpyxl.load_workbook('example.xlsx')
# 在最后一个sheet的前面添加新sheet
last_sheet = workbook.sheetnames[-2] # 获取倒数第2个sheet的名字
new_sheet = workbook.create_sheet('新的Sheet', -1)
# 将每组数据逐行写入新的sheet中
for name, group in grouped:
for row in group.itertuples(index=False):
new_sheet.append(list(row))
# 保存excel文件
workbook.save('example.xlsx')
```
这个代码块会读取名为"new_data.csv"的CSV文件,按照"表名"和"sheet名"这两列进行分组,并将每组数据逐行写入到现有的Excel文件(名为"example.xlsx")的倒数第2个sheet之前的新sheet中。请注意,在运行代码时,需要将分组的两个列名修改为您实际使用的列名。谢谢!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)