Home > OS >  How do I get this for loop to create one dataframe, instead of creating a new one each time it loops
How do I get this for loop to create one dataframe, instead of creating a new one each time it loops

Time:01-02

So, I am trying to return a dataframe of the values that are separated by a - or a / in my original dataframe. It does this, however seems to create a new df every time and I can't concatenate them either?

lst = []
for item in ref100.Class:
    if len(item) > 3:
        result = re.sub("[^0-9]", " ", item)
        lst1 = result.split(' ')
        lst = pd.DataFrame(lst1)
        lst.replace('', np.nan, inplace = True)
        lst.dropna(inplace = True)
        print (lst)

which returns:

    0
1  43
2  44
    0
1  45
2  47

but I want

    0
1  43
2  44
3  45
4  47

Any help would be appreciated!

CodePudding user response:

Consider generalizing your process in a defined method. Then build a list of data frames to be concatenate together at the end:

def build_frame(item):
    result = re.sub("[^0-9]", " ", item) 
    lst1 = result.split(' ') 
    df = pd.DataFrame(lst1).replace('', np.nan).dropna()
    
    return df
    
final_df = pd.concat(
     [build_frame(item) for item in ref100["Class"] if len(item) > 3]
)
  • Related