0

i am working on a pandas dataframe which is the image of this csv file :

enter image description here

as you can see, lines 4,5 and 6 represents a time, I would like to merge those three lanes in order to get a dataframe like this :

enter image description here

So the question is : What is the most efficient way to do this ? I've been thinking about :

allRows=[]
for row in df.itertuples():
    allRows.append(row)
        
i=0
while (i<len(allRows)):
    if("time" in allRows[i][4]):
        ts=str(allRows[i][5])+":"+str(allRows[i+1][5])+":"+str(allRows[i+2][5]) #concatenate time data
        label=allRows[i][1].split(".")[0]+"."+allRows[i][1].split(".")[1] #creation of the new label
        df.set_value(i,'Result',ts)
        df.set_value(i,'src_label_designation', label)
        df.drop([i+1,i+2]) #suppression of useless rows
    i=i+1

i am not really familiar to pandas dataframes so i know my solution looks pretty ugly so if you can do it better i'll take it :)

FrozzenFinger
  • 1,482
  • 2
  • 14
  • 35
  • groupby dest_label and aggregate - make a customer function for the aggregation to paste the strings as you wish. – Areza Jul 01 '20 at 12:59
  • @user702846 what do you mean by aggregate ? can you detail a bit more your idea ? thx for your time :) – FrozzenFinger Jul 01 '20 at 13:00
  • df.groupby('dest_label').agg() - similar to https://stackoverflow.com/questions/27298178/concatenate-strings-from-several-rows-using-pandas-groupby - I am not sure if this is the most efficient way, but def. a way to go – Areza Jul 01 '20 at 13:11
  • thank you very much i'll take a look at it ! – FrozzenFinger Jul 01 '20 at 13:12

0 Answers0