Python Forum

Full Version: How to combine multiple rows of strings into one using pandas?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a DataFrame with multiple rows. Is there any way in which they can be combined to form one rows? Marked with Yellow Colours. All I want to combine that into one row.
Remember that I wanted to ignore the empty rows when you combine.
See the output section of attached image("Problem. jpg"). I want my output looks like this.
But I cannot find logic to this problem. Any idea?
Attach File.
Hope you can see such output from this code

import re

for i in range(Data_list.shape[0]): #iterate over rows
    for j in range(Data_list.shape[1]): #iterate over columns
        if ((i ==0) & (j ==0)):
            heading = re.sub(r'[^\x00-\x7f]',r'', Data_list.at[i, j]) #get cell value
            print(heading, end="\n")
        else:
            if pd.notnull(Data_list.at[i, j]):
                data = re.sub(r'[^\x00-\x7f]',r'', Data_list.at[i, j]) #get cell value
                print(data, end=", ")
Output:
Comments & Equip. Transfers Drilling good ground all shift, 2 x Gyro Surveys, Mixing muds to condition the hole, Driller travelled home for shift change at end of shift, Equipment onsite=,