find certain string , broke the row and create a new row/rows

Question:

Hi i have string which i need to find certain strings and create a new rows within the data frame
ex: data – find "Number of days" and create new row same as "Appx. average wt"

data = {'Name': ['112 13 15 195 46 60 7 28 88 73 16 91 9  Number of days 32 30 29 32 30 30 31 32 31 29 30 31 30  Appx. average wt 39 4 40 74 331 302 273 263 277 274 307 295 303']}
data = pd.DataFrame(data) 
data_out = {'Name': ['112 13 15 195 46 60 7 28 88 73 16 91 9',  'Number of days 32 30 29 32 30 30 31 32 31 29 30 31 30',  'Appx. average wt 39 4 40 74 331 302 273 263 277 274 307 295 303']}
data_out = pd.DataFrame(data_out)

Thanks in advance.

Asked By: san1

||

Answers:

Try as follows:

  • Use str.split with a regex pattern and set expand to True to get the result in separate columns. The regex used below checks for one or more whitespaces (s+), followed by a uppercase letter (A-Z).
  • Now, use .T to transpose the result, turning the columns into rows.
  • Finally, use df.rename to rename the one column that we end up with (it’s name will be 0).
res = data.Name.str.split(r's+(?=[A-Z])', expand=True, regex=True).T
    .rename(columns={0:'Name'})
print(res)

                                                Name
0             112 13 15 195 46 60 7 28 88 73 16 91 9
1  Number of days 32 30 29 32 30 30 31 32 31 29 3...
2  Appx. average wt 39 4 40 74 331 302 273 263 27...
Answered By: ouroboros1
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.