组合多个文件夹的数据

q1qsirdb  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(191)

我有两个文件夹(q&p)的文件(>10)和数据。我想自动分析文件夹中所有匹配的文件。这意味着p1和q1,p5和q5等。我没有错误,但我没有得到我想要的结果。我想这是因为我的循环没有记住中间步骤(只记住文件夹中最后一个文件的操作),对吗?在文件夹的初始循环之后,如何分析所有匹配的文件?非常感谢你的帮助!

p = [] #empty list for results precipiation loop
q = [] #empty list for results discharge loop

# Preciptation folder read

for root, dirs, files in os.walk(r"C:TSA_P"):
    for filename in files:
        file_path = os.path.join(root, filename)
        with open(file_path) as file:
            file = pd.read_csv(file, delimiter=' ', header=None, skiprows=1, names=['year','month','day','precipitation_cpc'])
            file.columns = file.columns.str.replace(' ', ',')
            file.loc[:,'dt'] = pd.to_datetime(file[['year', 'month', 'day']])
            file.index = file['dt']
            file.columns = ['year', 'month', 'day','Rain','dt']
            p.append(file)

# file.head()

# Discharge folder read

for root, dirs, files in os.walk(r"C:\TSA_Q"):
    for filename in files:
        file_path = os.path.join(root, filename)
        with open(file_path) as f:
            f = pd.read_csv(f, delimiter=' ', header=None, skiprows=1, names=['year','month','day','streamflow_m3s', 'qual_control_by_ana', 'qual_flag'])
            f.columns = f.columns.str.replace(' ', ',')
            f.loc[:,'dt'] = pd.to_datetime(f[['year', 'month', 'day']])
            f.index = f['dt']
            f.columns = ['year', 'month', 'day','Discharge','Quality','Flag','dt']
            f = f.loc[(f.index.year>=1980)]
            q.append(file)

# f.head()

# Part C

C = pd.DataFrame() #Data P & Q per catchment
Format = (24*3600) #mm/d to mm/s
C['Psum'] = file['Rain'].resample('Y').sum()/Format #Daily to yearly
C['Qsum'] = f['Discharge'].resample('Y').sum() #Daily to yearly

# C.head()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题