Merging data pieces from separate, fictive sensors into common observations

temp_C = [ ['12:00', 14], ['12:15', 15], ['12:30', 16], ['12:45', 16], ['13:00', 15], ['13:15', 15], ['13:30', 17] ] humidity_perc = [ ['12:00', 77], ['12:15', 74], ['12:30', 75], ['12:45', 68], ['13:00', 70], ['13:15', 72], ['13:30', 75] ] wind_m_s = [ ['12:00', 4.2], ['12:15', 4.0], ['12:30', 3.7], ['12:45', 3.4], ['13:00', 3.2], ['13:15', 3.3], ['13:30', 3.5] ] # Goal: Merge measurements on time -> ('12:00', 14, 77, 4.2)... # Variant 1 def merge_seqs(temp_C, humidity_perc, wind_m_s): if len(temp_C) != len(humidity_perc) != len(wind_m_s): raise ValueError('All sequences must be of equal length.') res = [] for temp, hum, wind in zip(temp_C, humidity_perc, wind_m_s): time1, t = temp time2, h = hum time3, w = wind if time1 == time2 == time3: res.append((time1, t, h, w)) return res print(merge_seqs(temp_C, humidity_perc, wind_m_s)) # Variant 2 import numpy as np temp_C = np.array(temp_C) humidity_perc = np.array(humidity_perc) wind_m_s = np.array(wind_m_s) times1 = temp_C[:, 0] times2 = humidity_perc[:, 0] times3 = wind_m_s[:, 0] assert(len(set(times1) | set(times2) | set(times3)) == len(times1) == len(times2) == len(times3)) print([[temp_C[i, 0], temp_C[i, 1].astype(int), humidity_perc[i, 1].astype(int), wind_m_s[i, 1].astype(float)] for i, t in enumerate(times1)]) # Variant 3 def get_col(seq, col, data_type): return list(map(lambda x: data_type(x[col]), seq)) times1 = get_col(temp_C, 0, str) times2 = get_col(temp_C, 0, str) times3 = get_col(temp_C, 0, str) assert(times1 == times2 and times2 == times3 and times3 == times1) temps = get_col(temp_C, 1, int) hum = get_col(humidity_perc, 1, int) wind = get_col(wind_m_s, 1, float) print([(times1[i], temps[i], hum[i], wind[i]) for i in range(len(temp_C))]) # Variant 4 import pandas as pd def merge_all(cols): res = pd.merge(cols[0], cols[1], on=0) for i in range(2, len(cols)): res = pd.merge(res, cols[i], on=0) return res temp_df = pd.DataFrame(temp_C) hum_df = pd.DataFrame(humidity_perc) wind_df = pd.DataFrame(wind_m_s) temp_df[1] = temp_df[1].astype(int) hum_df[1] = hum_df[1].astype(int) wind_df[1] = wind_df[1].astype(float) merged = merge_all([temp_df, hum_df, wind_df]) print(merged.as_matrix().tolist()) """ Result of all variants: [['12:00', 14, 77, 4.2], ['12:15', 15, 74, 4.0], ['12:30', 16, 75, 3.7], ['12:45', 16, 68, 3.4], ['13:00', 15, 70, 3.2], ['13:15', 15, 72, 3.3], ['13:30', 17, 75, 3.5]] """