[英]Open many txt files and sort into two dfs
我需要根據它們的名稱和其中包含的文件夾名稱,將數百個.txt文件打開並處理為兩個數據幀。
文件夾結構:
我有一個文件夾,其中包含許多子文件夾,每個子文件夾2019-0-14
記錄數據的日期命名,格式為: YYY-MM-DD
,例如: 2019-0-14
文件結構:
在上述每個文件夾中,有576個文件。 有兩組測量值(基於2個位置),在每24小時內每5分鍾進行一次測量(12 * 24 * 2 = 576)。 這些文件的名稱如下:
hhmmssILAC3octf.txt for the indoor location
hhmmssOLAC3octf.txt for the outdoor location
其中hhmmss
是每個5分鍾文件的小時,分鍾和秒, IL
在室內, OL
在戶外。
文件內容:
每個文件包含5行數據,每分鍾一行。 此數據是相同類型的數據和相同長度的數據,用逗號分隔。
我想要達到的目標:
我需要創建兩個數據框:每個位置一個,以日期(文件夾名稱)和時間(文件名和位置[行1:5])作為日期時間索引,基於其中包含的文件夾,名稱.txt中的文件和行號
我還需要重新命名所有導入的列/變量,並使用相同的名稱,但要根據其位置在室內或室外添加前綴。 例如:indoor_20hz。
我自己使用Python和Pandas,但從未嘗試解決此類問題。 請有人能指出正確的方向...
謝謝。
您可以從以下代碼開始:
import os
import fnmatch
start_dirctory='.' # change this
df_result= None
for path, dirs, files in os.walk(start_dirctory):
for file in fnmatch.filter(files, '*.txt'):
full_name=os.path.join(path, file)
df_tmp= pd.read_csv(full_name)
# add the line number
df_tmp['line_number']= range(df_tmp.shape[0])
# add the code here that generates the infos
# you additionally need here to the df
# then concatenate the files together
if df_result is None:
df_result= df_tmp
else:
df_result= pd.concat([df_result, df_tmp], axis='index', ignore_index=True)
因此,您應將所有文件的內容包含在df_result
。 但是您需要確保文件具有相同的列結構,否則需要在上面進行修復。 您還需要添加所需的其他信息,以代替“#將此處需要的信息添加到df”。
我的最終解決方案,盡管我確信這不是獲得最終結果的最優雅的方法:
import os
import fnmatch
import pandas as pd
start_dirctory='DIR' # change this
df_result= None
for path, dirs, files in os.walk(start_dirctory):
for file in fnmatch.filter(files, '*.txt'):
full_name=os.path.join(path, file)
df_tmp= pd.read_csv(full_name, header=None)
df_tmp['date']=os.path.basename(path)
df_tmp['file']=os.path.basename(file)
# df_tmp.set_index([df_tmp['date'], df_tmp['time']], inplace=True)
# add the line number
df_tmp['line_number']= range(df_tmp.shape[0])
# add the code here that generates the infos
# you additionally need here to the df
# then concatenate the files together
if df_result is None:
df_result= df_tmp
else:
df_result= pd.concat([df_result, df_tmp], axis='index', ignore_index=True)
# Slice filename from 6 to 7 to get location
df_result['location'] = df_result['file'].str.slice(6,7)
# Slice filename from 0 to 6 to get time
df_result['time'] = df_result['file'].str.slice(0,6)
# Combine date and time and format as datetime
df_result['date'] = pd.to_datetime(df_result['date'] + ' ' + df_result['time'], errors='raise', dayfirst=False)
# Round all the datetimes to the nearest 5 min
df_result['date'] = df_result['date'].dt.round('5min')
# Add line number as minutes to the date
df_result['date'] = df_result['date'] + pd.to_timedelta(df_result['line_number'],unit='m')
del df_result['file']
del df_result['line_number']
del df_result['time']
# Make the date the index in df
df_result = df_result.set_index(df_result['date'])
# Delete date in df
del df_result['date']
# Change columns and rename df_result
df_result.columns = ['10hz', '12.5hz', '16hz', '20hz','25hz','31.5hz','40hz','50hz','63hz','80hz','100hz','125hz','160hz','200hz','250hz','315hz','400hz','500hz','630hz','800hz','1000hz','1250hz','1600hz','2000hz','2500hz','3150hz','4000hz','5000hz','6300hz','8000hz','10000hz']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.