我遇到以下需要幫助的問題。 我有一個csv文件中的310條記錄,其中包含一些關於錯誤的信息。 在另一個csv文件中,我有80萬條包含有關行李統計的記錄(可能導致錯誤的事件)。通過熊貓數據幀進行高效循環
與下面的腳本,我通過漏洞試圖
- 循環,並選擇一個。
- 循環通過統計記錄和檢查一些條件
- 如果有匹配,從錯誤記錄統計 記錄添加一列。
- 另存新文件
我的問題是,如果我能在使用numpy的或任何其他更有效的方式archieve這一點。 目前的方法是採取永遠因爲統計
任何幫助或提示在正確的方向的大小的運行將不勝感激。 感謝名單中adavance
dataset = pd.read_csv('310_records.csv')
dataset1 = pd.read_csv('800K_records.csv')
cols_error = dataset.iloc[:, [0, 1, 2, 3, 4, 5, 6]]
cols_stats = dataset1.iloc[:, [1, 2, 3, 4, 5, 6, 7, 8, 9]]
cols_stats['Fault'] = ''
cols_stats['Created'] = ''
for i, error in cols_error.iterrows():
fault_created = error [0]
fault_ucs = error [1]
fault_dn = error [2]
fault_epoch_end = error [3]
fault_epoch_begin = error [4]
fault_code = error [6]
for index, stats in cols_stats.iterrows():
stats_epoch = stats[0]
stats_ucs = stats[5]
stats_dn = stats[7]
print("error:", i, " Stats:", index)
if(stats_epoch >= fault_epoch_begin and stats_epoch <= fault_epoch_end):
if(stats_dn == fault_dn):
if(stats_ucs == fault_ucs):
cols_stats.iloc[index, 9] = fault_code
cols_stats.iloc[index, 10] = fault_created
else:
cols_stats.iloc[index, 9] = 0
cols_stats.iloc[index, 10] = fault_created
cols_stats.to_csv('datasets/dim_stats_error.csv', sep=',', encoding='utf-8')
可能會被很多不說'print'聲明更快。 –
@StefanPochmann注意到..我會擺脫那條線。 Thanx – Makten
您可以在800k_records中搜索並隔離第三個文件中的所有錯誤(保留位置跟蹤)。並在第三個文件中進行匹配?此外,並行執行多個搜索將有助於 – pwnsauce