如何以更好的方式聚合多个DataFrame?

时间:2019-03-13 21:54:13

标签: python-3.x dataframe merge

所以我有36个数据框,我必须在这三列['Sensor ID','Time Instant','Measurement']中合并一个数据框

所以,这就是我所做的:

mi_pollution_1 = pd.read_csv('/content/drive/My Drive/DatiAirQuality/MI_Air_Quality/data/mi_pollution_10273.csv' )
mi_pollution_1.columns= ['Sensor ID' , 'Time Instant' , 'Measurement']
mi_pollution_2 = pd.read_csv('/content/drive/My Drive/DatiAirQuality/MI_Air_Quality/data/mi_pollution_10278.csv')
mi_pollution_2.columns= ['Sensor ID' , 'Time Instant' , 'Measurement']
mi_pollution_3 = pd.read_csv('/content/drive/My Drive/DatiAirQuality/MI_Air_Quality/data/mi_pollution_10279.csv')
mi_pollution_3.columns= ['Sensor ID' , 'Time Instant' , 'Measurement']

.
.
.
.
mi_pollution_35= pd.read_csv('/content/drive/My Drive/DatiAirQuality/MI_Air_Quality/data/mi_pollution_6372.csv')
mi_pollution_35.columns= ['Sensor ID' , 'Time Instant' , 'Measurement']
mi_pollution_36= pd.read_csv('/content/drive/My Drive/DatiAirQuality/MI_Air_Quality/data/mi_pollution_6372.csv')
mi_pollution_36.columns= ['Sensor ID' , 'Time Instant' , 'Measurement']

然后:

 frames = [mi_pollution_1 , mi_pollution_2 ,mi_pollution_3 , mi_pollution_4,
     mi_pollution_5 , mi_pollution_6,mi_pollution_7 , mi_pollution_8,
     mi_pollution_9 , mi_pollution_10,mi_pollution_11 , mi_pollution_12, 
     mi_pollution_13 , mi_pollution_14, mi_pollution_15 , mi_pollution_16,
     mi_pollution_17 , mi_pollution_18 ,mi_pollution_19 , mi_pollution_20,
     mi_pollution_21 , mi_pollution_22,mi_pollution_23 , mi_pollution_24,
     mi_pollution_25 , mi_pollution_26,mi_pollution_27 , mi_pollution_28, 
     mi_pollution_29 , mi_pollution_30, mi_pollution_31 , mi_pollution_32,
     mi_pollution_33 , mi_pollution_34, mi_pollution_35 , mi_pollution_36]

 df_result = pd.merge(frames , on = ['Sensor ID' , 'Time Instant' , 'Measurement'])

所以,我想知道是否有一种更有效,更“清洁”的方法来实现它。 谢谢

1 个答案:

答案 0 :(得分:0)

如果您的文件内容是同质的,看起来像是基于您上面的示例,则我认为将数据首先加载为列表可能更干净。

EventHandler/Filter