簡體   English   中英

在數據框中切換到多行索引創建的列表列表

[英]Switching to multi-row index in dataframe created form list of lists

我有一個從列表列表創建DataFrame的函數:

def logs_reader():
    path = Path("C:\\Users\\" + getpass.getuser() + "\\DCBviz\\logs\\")

cols1 = ['Station ID', 'Reciever type', 'Satellite system', 'Date installed', 'Date removed']
cols2 = ['Station ID', 'Antenna type', 'Cable length', 'Date installed', 'Date removed']

file_list = [f for f in path.glob('**/*.log') if f.is_file()]
receivers_data = []
antennas_data = []
for file in file_list:
    with open(file, encoding='utf8') as f:
        contents = f.read()
        station_id = re.findall("Four Character ID\s*:\s*(.*?)\s*$", contents, re.MULTILINE)
        
        receiver_types = re.findall("Receiver Type\s*:\s*(.*?)\s*$", contents, re.MULTILINE)
        satellite_sys = re.findall("Satellite System\s*:\s*(.*?)\s*$", contents, re.MULTILINE)
        date_installed = re.findall("Date Installed\s*:\s*(.*?)T.*$", contents, re.MULTILINE)
        date_removed = re.findall("Date Removed\s*:\s*(.*?)T.*$", contents, re.MULTILINE)
        
        antenna_types = re.findall("Antenna Type\s*:\s*(.*?)\s.*$", contents, re.MULTILINE)
        cable_lengths = re.findall("Antenna Cable Length\s*:\s*([0-9]+\.*[0-9]*)\s.*$", contents, re.MULTILINE)
        antenna_date_installed = re.findall("Date Installed\s*:\s*(.*?)T.*$", contents, re.MULTILINE)
        antenna_date_removed = re.findall("Date Removed\s*:\s*(.*?)T.*$", contents, re.MULTILINE)
        
        receivers_data.append([station_id, receiver_types, satellite_sys, date_installed, date_removed])
        antennas_data.append([station_id, antenna_types, cable_lengths, antenna_date_installed, antenna_date_removed])
        
        d = []
        
        for l in receivers_data:
            d.append({'Station ID': l[0]*len(l[1]), 
                  'Reciever type': l[1], 
                  'Satellite system': l[2], 
                  'Date installed': l[3][0:len(l[1])],
                  'Date removed': l[4][0:len(l[1])]})
        df = pd.DataFrame(d)   
return df

df = logs_reader()

作為回報,我有如下所示的數據框: 在此處輸入圖片說明

我想從 2-6 列中拆分列表,並使用Station ID作為多行索引創建純字符串的單個條目。 我怎樣才能做到這一點?

期望的輸出:

在此處輸入圖片說明

所以你在列表中有你的正則數據

receiver_types 
satellite_sys 
date_installed
date_removed
    
antenna_types
cable_lengths
antenna_date_installed
antenna_date_removed

現在,我假設每個文件對應一個station_id ,還行station_id = re.findall(...會返回一個列表。

然后你將有一個station_id ,它是一個長度為 1 的列表和一堆其他列表。 如果所有的接收器列表是相同lenght的,你可以創建一個DF並將其收集在receivers_data用下面的代碼(請我刪除周圍的括號再次復制station_id )。 然后對antennae_data執行相同的操作。

請注意,您當前的代碼在每次迭代時都丟棄df並且只從讀取的最后一個文件中返回數據。


正如我在評論中提到的,如果同一行中的所有列表的長度都相同,那么最好的選擇是從每個文件創建一個 df 並在循環后將它們連接起來

你可以更換線

receivers_data.append([station_id, receiver_types, satellite_sys, date_installed, date_removed])

receivers_data.append(
    pd.DataFrame(
        [station_id * len(receiver_types), receiver_types, satellite_sys, date_installed, date_removed],
        columns=list_of_column_names
    )
)
# or instead of a list use a dict with file_name as keys

閱讀完所有文件后,您可以將兩個列表連接起來

df_receivers = pd.concat(receivers_data)
df_antennae = pd.concat(antennae_data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM