Excel有一个选项卡,Sheet1 (61列,20k行),需要选择在第5列(列名为Country)中值为'UK‘,在第38列(列名为Status)中值为'Yes’的所有行。然后,所有这些经过过滤的行都复制到新的Sheet2中。
发布于 2021-03-23 20:29:01
如果以后在操作此文件时不想删除旧的工作表,请使用此命令。Pandas是一个python库,它可以帮助你(但不限于)使用excels和csv。
import pandas as pd
from openpyxl import load_workbook
df = pd.read_excel('Path/to/folder/filename.xlsx')
filtered_df = df[df['Country']=='UK' & df['Status']=='Yes']
book = load_workbook('Path/to/folder/filename.xlsx')
writer = pd.ExcelWriter('Path/to/folder/filename.xlsx', engine = 'openpyxl')
writer.book = book
filtered_df.to_excel(writer, sheet_name = 'sheet2')
writer.save()
writer.close()
发布于 2021-03-23 20:35:31
您可以使用Pandas完成所有这些操作。首先读取excel文件,然后过滤数据帧并保存到新工作表中。
import pandas as pd
df = pd.read_excel('file.xlsx', sheet_name=0) #reads the first sheet of your excel file
df = df[(df['Country']=='UK') & (df['Status']=='Yes')] #Filtering dataframe
df.to_excel('file.xlsx', sheet_name='Filtered Data') #Saving to a new sheet called Filtered Data
https://stackoverflow.com/questions/66770678
复制相似问题