Search code examples
pythonlistdataframefor-loopconcatenation

Looping through paired list to merge rows of Data Frame in python


I have a data frame where all of the cells are string. I want to loop through a paired list and concatenate the rows of the data frame.

For example, I have a data frame that looks like this:

df=[['7','4','5','',''],["","","",'7','4'],['9','4','7','8','4'],["","","",'7','5'],['4','8','5','4','6'],['4','9','41','74','20'],['4','Super','yes','0','9']]
df=pd.DataFrame(df)
df.astype('str')
print(df)
0   1   2   3   4   
7   4   5           
            7   4
9   4   7   8   4
            7   5
4   8   5   4   6
4   9   41  74  20

I want to loop through the paired list below, where the first element is a start and a second end.

list_index=[[1,3],[4,5]]

This is my trial. I tried to change the list into range so that I can access each pared elements and loop through them, but no luck throwing TypeError: 'list' object cannot be interpreted as an integer.

for row,index in df.iterrows():
    for i in range(len(list_index)):
        for j in range(list_index[i]):
            df[row,:] = df[row,:] + row 

Finally, for the start and end of indices I have above, I want the data final data frame to look like the this:

0   1   2   3   4   
7   4   5           
9   4   7   787 445
44  89  541 474 620

I am not sure how to loop through a paired elements of list in python. Can anyone help me with this?


Solution

  • Here is abother solution without the groupby

    start_end = [[1,3],[4,5]]
    
    new_df = pd.DataFrame()
    
    for l in start_end:
    
        temp_df = df.iloc[l[0] : l[1] + 1,:].apply(lambda x : ''.join(x)).to_frame().transpose()
    
        new_df = pd.concat([new_df, temp_df], ignore_index = True)
    
    new_df