简体   繁体   中英

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

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.

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 ( AZ ).
  • 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM