Background
There is 5 DataFrames, I will call them b1, b2, b3, b4, b5
.
They have data structure which consists of the columns ['Date', 'Value']
I got the data from 2014 to 2015.
Problem
Every DataFrame has different date counting system. So, I want to get rid of the rows in the DataFrames which have non-matching dates.
How can I do that?
What I have tried
So, I will delete all the data if Date
of the data is not included in all DataFrames: b1, b2, b3, b4, b5
This works for the first time
for i in range(len(b2.index)):
k = 0
for j in range(len(b1.index)):
if b2['Date'][i] == b1['Date'][j]:
k = k+1
else:
k = k
if k == 1:
pass
if k == 0:
b2 = b2.drop([i])
But, after that I excute this code one more time after I did that, there would be some error like this:
KeyError Traceback (most recent call last) in () 2k = 0 3for j in range(len(b2.index)): ----> 4 if b1['Date'][i] == b2['Date'][j]: 5 k = k+1 6 else:
C:\Users\cms\Anaconda\lib\site-packages\pandas\core\series.pyc in getitem(self, key) 519def getitem(self, key): 520 try: --> 521 result = self.index.get_value(self, key) 522 523 if not np.isscalar(result):
C:\Users\cms\Anaconda\lib\site-packages\pandas\core\index.pyc in get_value(self, series, key) 1593 1594 try: -> 1595 return self._engine.get_value(s, k) 1596 except KeyError as e1: 1597 if len(self) > 0 and self.inferred_type in ['integer','boolean']:
pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:3113)()
pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:2844)()
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3704)()
pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7224)()
pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:7162)()
KeyError: 28L
What I want to do is
mlist = (b1,b2,b3,b4,b5)
for q in mlist:
for r in mlist:
for i in range(len(q.index)):
k = 0
for j in range(len(r.index)):
if q['Date'][i] == r['Date'][j]:
k = k+1
else:
k = k
if k == 1:
pass
if k == 0:
q = q.drop([i])`enter code here`