Reputation: 91
so I have a list of dataframes as such:
yldDF = [dem2y, dem5y, dem10y, dem30y]
Unnamed: 0 BBK01.WT0202
4 2014-01-02 0.21
5 2014-01-03 0.21
6 2014-01-04 .
7 2014-01-05 .
8 2014-01-06 0.20
9 2014-01-07 0.21
10 2014-01-08 0.21
11 2014-01-09 0.23
12 2014-01-10 0.21
13 2014-01-11 .
Where each dataframe looks like the above. Now when I try to iterate through the dataframes to remove the "." in the second column it doesn't seem to work.
dem2y = pd.read_csv("0202.csv")
dem5y = pd.read_csv("0505.csv")
dem10y = pd.read_csv("1010.csv")
dem30y = pd.read_csv("3030.csv")
### Begin cleaning
yldDF = [dem2y, dem5y, dem10y, dem30y]
for i in yldDF:
i.drop(i.columns[2], axis = 1,inplace = True)
i.drop(range(4),inplace=True)
i = i[i[i.columns[1]].apply(lambda x: str(x)!=".")]
I've tried the last line on a single Dataframe and it seems to work perfectly fine, however when I try it as part of a loop it doesn't seem to have any effect. Is there something i'm missing here? Or could this be a bug?
Upvotes: 0
Views: 49
Reputation: 2415
If the problem is that only i
is getting modified, you could try this trick:
# Store your `pandas.DataFrame` in a dict
yldDict = {'dem2y': dem2y, 'dem5y': dem5y, 'dem10y': dem10y, 'dem30y': dem30y}
# Loop on the keys of the dict
for keys in yldDict.keys():
df = yldDict.get(keys, 'error') # Get pandas.DataFrame from dict
# Do the operation you need to do
df.drop(df.columns[2], axis = 1, inplace = True)
df.drop(range(4), inplace=True)
df = df[df[df.columns[1]].apply(lambda x: str(x)!=".")]
# Post to dict
yldDict[keys] = df
# Extract from dict
dem2y, dem5y, dem10y, dem30y = yldDict['dem2y'], yldDict['dem5y'], yldDict['dem10y'], yldDict['dem30y']
# Delete dict
del yldDict
Upvotes: 1