Wednesday, 16 March 2022

how do you go though data frame values in chunks and combine them?

I have this data frame:

Metric  ProcId  TimeStamp               Value
CPU     proce_123   Mar-11-2022 11:00:00    1.4453125
CPU     proce_126   Mar-11-2022 11:00:00    0.058320373
CPU     proce_123   Mar-11-2022 11:00:00    0.095274389
CPU     proce_000   Mar-11-2022 11:00:00    0.019654088
CPU     proce_144   Mar-11-2022 11:00:00    0.019841269
CPU     proce_1     Mar-11-2022 11:00:00    0.234741792
CPU     proce_100   Mar-11-2022 11:00:00    5.32945776
CPU     proce_57777 Mar-11-2022 11:00:00    0.25390625
CPU     proce_0000  Mar-11-2022 11:00:00    0.019349845
CPU     proce_123   Mar-11-2022 11:00:00    0.019500781
CPU     proce_123   Mar-11-2022 11:00:00    2.32421875
CPU     proce_123   Mar-11-2022 11:00:00    68.3903656
CPU     proce_123   Mar-11-2022 11:00:00    0.057781201
CPU     proce_123   Mar-11-2022 11:00:00    0.416666627

this is just a sample data frame, the actual data frame is in thousands of rows. I need to go though this data frame in chunks the "ProdID" column and I need to create a string combining these ProdID in chunks for each iteration.

For example the string needs to be like this given the chunks size 3:

proce_000%22%2C%2proce_144%22%2C%2proce_1%22%29
proce_100%22%2C%2proce_57777%22%2C%2proce_0000%22%29
proce_123%22%2C%2proce_126%22%2C%2proce_111%22%29

Please note after the 3rd chunk, we need to add "%22%29". After the first ad second we need to add "%22%2C%2".

I can do something like this to print out the chunks:

n = 3 #size of chunks
chunks = [] #list of chunks

for i in range(0, len(id), n): 
    chunks.append(id[i:i + n])

I am not sure how would I combine these 3 items in one string and add the others strings at the end. Can anybody help here?



from how do you go though data frame values in chunks and combine them?

No comments:

Post a Comment