I combined a set of dataframes into a list for iterating over for formatting purposes. I am able to run multiple methods froms pandas on these and it translates to the dataframe, but when I try to apply conditional selection in the rows it is done on the element level in the for loop but not on the actual dataframe itself. I can easily just write a line of code for each dataframe but this seems redundant when I have several steps to apply to both dataframes (steps afterwards not shown). Also, this list will grow and I feel there has to be a way to filter these without having to type a line for each time I want to filter rows.
bgn = '19991231'
end = '20211130'
dummy = [vg_chain,nk_chain]
for x in dummy:
x.columns = ['Ticker','Last Trade']
x.reset_index(drop=True,inplace=True)
x['Str Date'] = x['Last Trade'].apply(lambda x: x.strftime('%Y%m%d'))
#loop not working with conditional selection for some reason. revisit
x = x[(x['Str Date'] >= bgn) & (x['Str Date'] <= end)]
In the above example each dataframe is in the dummy list. The methods all work except the last line of code filtering the string date between bgn and end. It filters the rows at variable x but does not translate to the actual dataframe names like the other methods. I tried tinkering with the global variable and my suspicion is that it has to do with that but I have hit a wall.
I am currently using below to filter:
vg_chain = vg_chain[(vg_chain['Str Date'] >= bgn) & (vg_chain['Str Date'] <= end)]
nk_chain = nk_chain[(nk_chain['Str Date'] >= bgn) & (nk_chain['Str Date'] <= end)]
Would appreciate any help you can provide.