简体   繁体   English

如何合并多个数据帧并按时间戳对它们进行排序 - Pandas Python

[英]How merge multiple dataframe and sort them by timestamp - Pandas Python

I open all the data files, I transform them into dataframe and I make a list of them.我打开所有数据文件,将它们转换为数据框,然后列出它们。 Each dataframe has the same structure.每个数据帧都具有相同的结构。

Here the code :这里的代码:

path = r'/home/afdg/Documents/Partage_Ubuntu/XML/Results'
all_csv_files = glob.glob(path + "/*.csv")

list_df = []

for filename in all_csv_files :
  df = pd.read_csv(filename, usecols=['duration', 'begin', 'end', 'time' ])
  df_DisponibilityAlarm = df.copy()
  df_DisponibilityAlarm = df_DisponibilityAlarm.drop(['duration','time'], 1)
  list_df.append(df_DisponibilityAlarm)

I would like to merge all the dataframe into one and sort by timestamp.我想将所有数据帧合并为一个并按时间戳排序。 I tried this but it doesn't work.我试过这个,但它不起作用。 I don't know how to connect my list of dataframes with the sorting.我不知道如何将我的数据框列表与排序联系起来。 :

df_DisponibilityAlarm = (df_DisponibilityAlarm.stack()
  .rename_axis([None, 'Flag'])
  .reset_index(level=1, name='Timestamp'))

df_DisponibilityAlarm = df_DisponibilityAlarm.sort_values(by=['Timestamp'])

When I use concat list I have more rows than the original files : The shape of the files :当我使用 concat list 时,我的行比原始文件多:文件的形状:

(358, 2)                                                                                             
(1690, 2)                                                                                            
(508, 2)                                                                                             
(3872, 2)                                                                                            
(13129, 2)                                                                                           
(2, 2)                                                                                               
(46, 2)                                                                                              
(92, 2)   

Result : (19697,2) and after : (19708, 2)结果:(19697,2) 和之后:(19708, 2)

11 rows in more 11 行更多

Can you help me please ?你能帮我吗 ?

您应该连接数据框列表:

df_all = pd.concat(list_df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM