Note: This is a toy example that will hopefully illustrate what I am trying to achieve.
I have a list of strings that I separate into two sub-lists in order to perform different preprocessing steps on. Lets say I have the following list of strings:
mylist = ['a1','a','a2','b','b2','b3','c','c1','c2']
For simplicity, I want to add a particular sub-string to the beginning of each element depending on whether that element contains a number (in reality, I have a multiple preprocessing steps that necessitates splitting the original list):
import re
withNum = [[i,'numPresent_'+i] for i in mylist if re.compile(r'd').search(i)]
noNum = [[i,'noNum_'+i] for i in mylist if not re.compile(r'd').search(i)]
Now that I have my two sub-lists, how can I combine them in a data-frame in a manner that they reflect their original order? Clearly, if I use df.append
it will simply stack one on top of the other...
df = pd.DataFrame().append(withNum).append(noNum)
Returns:
-------------------------
0 1
-------------------------
a1 numPresent_a1
a2 numPresent_a2
b2 numPresent_b2
b3 numPresent_b3
c1 numPresent_c1
c2 numPresent_c2
a noNum_a
b noNum_b
c noNum_c
--------------------------
How can I re-order the data-frame so that it reflects the order of the original list?
Intended Outcome:
-------------------------
0 1
-------------------------
a1 numPresent_a1
a noNum_a
a2 numPresent_a2
b noNum_b
b2 numPresent_b2
b3 numPresent_b3
c noNum_c
c1 numPresent_c1
c2 numPresent_c2
--------------------------
I cannot rely on the content of the string itself to inform its position (so sorting alphabetically is out). I can only rely on its original position in the list. I'm hoping there is someway I can create an index that I can sort by after I have merged the two sub-lists.
question from:https://stackoverflow.com/questions/65601470/restoring-original-order-of-python-list-after-processing