gpt4 book ai didi

python - 使用pandas包在python中组合来自多个excel文件的数据

转载 作者:行者123 更新时间:2023-11-28 22:11:23 25 4
gpt4 key购买 nike

我正在尝试将具有不同日期的 excel 数据文件合并到一个文件中,以便我可以使用 pandas 包进行分析。我遇到了困难,因为文件是按日期命名的,并且里面有多个工作表。

This is how the inside of the file looks like This is how the inside of the month folder looks like The inside of the year folder with multiples directoties

这是一项分析日期和绘制各种参数(即温度、大气温度、GHI 等)到天数/小时/分钟数的作业

import pandas as pd
import glob

all_data = pd.DataFrame() #Creating an empty dataframe
for f in glob.glob("/Data-Concentrated Solar Power-NamPower/Arandis 2016/2016 01 January/*.xlsx"): #path to datafiles and using glob to select all files with .xlsx extension
df = pd.read_excel(f)
all_data = all_data.append(df,ignore_index=True)


最佳答案

将每个文件 DataFrame 附加到列表中,然后使用 pandas.concat将它们全部组合成一个 DataFrame:

import pandas as pd
import glob

frames = []

for f in glob.glob("/home/humblefool/Dropbox/MSc/MSc Project/Data-Concentrated Solar Power-NamPower/Arandis 2016/2016 01 January/*.xlsx"): #path to datafiles and using glob to select all files with .xlsx extension
df = pd.read_excel(f).assign(file_name=f)
# Add date column for sorting later
df['date'] = pd.to_datetime(df.file_name.str.extract(r'(\d{4}-\d{2}-\d{2})', expand=False), errors='coerce')
frames.append(df)

all_data = pd.concat(frames, ignore_index=True).sort_values('date')

关于python - 使用pandas包在python中组合来自多个excel文件的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55727792/

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