gpt4 book ai didi

python - 删除多个 Excel 电子表格中的列

转载 作者:行者123 更新时间:2023-12-03 23:49:02 25 4
gpt4 key购买 nike

有没有办法在python中删除多个excel文件中的列?即我有一个包含多个 xlsx 文件的文件夹。每个文件大约有 5 列(日期、值、纬度、经度、区域)。我想删除每个 excel 文件中除日期和值之外的所有列。

最佳答案

假设您有一个包含多个 excel 文件的文件夹:

from pathlib import Path

folder = Path('excel_files')

xlsx_only_files = list(folder.rglob('*.xlsx'))


def process_files(xls_file):

#stem is a method in pathlib
#that gets just the filename without the parent or the suffix
filename = xls_file.stem

#sheet = None ensure the data is read in as a dictionary
#this sets the sheetname as the key
#usecols allows you to read in only the relevant columns
df = pd.read_excel(xls_file, usecols = ['date','value'] ,sheet_name = None)

df_cleaned = [data.assign(sheetname=sheetname,
filename = filename)
for sheetname, data in df.items()
]

return df_cleaned


combo = [process_files(xlsx) for xlsx in xlsx_only_files]

final = pd.concat(combo, ignore_index = True)

让我知道事情的后续

stem

关于python - 删除多个 Excel 电子表格中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60485563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com