Python Forum
How to combine multiple rows of strings into one using pandas? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to combine multiple rows of strings into one using pandas? (/thread-34657.html)



How to combine multiple rows of strings into one using pandas? - shantanu97 - Aug-18-2021

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.


RE: How to combine multiple rows of strings into one using pandas? - klllmmm - Aug-22-2021

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=,