1
我試圖用應用和lambda函數替換註釋掉的循環,但我得到下面的錯誤。我的蟒蛇是生鏽的,所以任何提示都非常感謝。替換循環與應用
錯誤:
File "<ipython-input-5-b29bfb93595e>", line 11
if (dataDF < dataDF.shift()) & (dataDF.shift(periods=1) < dataDF.shift(periods=2)):
^
語法錯誤:無效的語法
代碼:
def get_recession_end():
dataDF = pd.ExcelFile('gdplev.xls').parse(skiprows=7)[['Unnamed: 4', 'Unnamed: 5']].loc[246:]
dataDF.columns = ['Quarter','dataDF']
dataDF['dataDF'] = pd.to_numeric(dataDF['dataDF'])
#quarters = []
#for i in range(len(dataDF) - 2):
#if (dataDF.iloc[i][1] < dataDF.iloc[i+1][1]) & (dataDF.iloc[i+1][1] < dataDF.iloc[i+2][1]):
#quarters.append(dataDF.iloc[i+2][0])
#return quarters[0]
quarters = dataDF.apply(lambda x: quarters = []
if (dataDF < dataDF.shift()) & (dataDF.shift(periods=1) < dataDF.shift(periods=2)):
quarters.append(dataDF.shift(2)[0]))
return quarters[0]
get_recession_end()
更新新版本:
代碼:
def get_recession_end():
def get_recession_end():
dataDF = pd.ExcelFile('gdplev.xls').parse(skiprows=7)[['Unnamed: 4',
'Unnamed: 5']].loc[246:]#skiprows=17,skip_footer=(38))
dataDF.columns = ['Quarter','dataDF']
dataDF['dataDF'] = pd.to_numeric(dataDF['dataDF'])
#quarters = []
#for i in range(len(dataDF) - 2):
#if (dataDF.iloc[i][1] < dataDF.iloc[i+1][1]) & (dataDF.iloc[i+1][1]
< dataDF.iloc[i+2][1]):
#quarters.append(dataDF.iloc[i+2][0])
#return quarters[0]
def do_the_foo(x):
quarters = []
if (dataDF < dataDF.shift()) & (dataDF.shift(periods=1) <
dataDF.shift(periods=2)):
quarters.append(dataDF.shift(2)[0])
return quarters
quarters = dataDF.loc[:(len(dataDF) - 2)].apply(do_the_foo)
return quarters[0]
get_recession_end()
新的錯誤:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/opt/conda/lib/python3.5/site-packages/pandas/indexes/base.py in
get_loc(self, key, method, tolerance)
1944 try:
-> 1945 return self._engine.get_loc(key)
1946 except KeyError:
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(pandas/hashtable.c:12368)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(pandas/hashtable.c:12322)()
KeyError: 0
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-10-53e0a21f9faa> in <module>()
18
19
---> 20 get_recession_end()
<ipython-input-10-53e0a21f9faa> in get_recession_end()
15
16 quarters = dataDF.loc[:-(len(dataDF) - 2)].apply(do_the_foo)
---> 17 return quarters[0]
18
19
/opt/conda/lib/python3.5/site-packages/pandas/core/frame.py in
__getitem__(self, key)
1995 return self._getitem_multilevel(key)
1996 else:
-> 1997 return self._getitem_column(key)
1998
1999 def _getitem_column(self, key):
/opt/conda/lib/python3.5/site-packages/pandas/core/frame.py in
_getitem_column(self, key)
2002 # get column
2003 if self.columns.is_unique:
-> 2004 return self._get_item_cache(key)
2005
2006 # duplicate columns & possible reduce dimensionality
/opt/conda/lib/python3.5/site-packages/pandas/core/generic.py in
_get_item_cache(self, item)
1348 res = cache.get(item)
1349 if res is None:
-> 1350 values = self._data.get(item)
1351 res = self._box_item_values(item, values)
1352 cache[item] = res
/opt/conda/lib/python3.5/site-packages/pandas/core/internals.py in get(self,
項目,快速路徑) 3289如果不是ISNULL(項目): - > 3290 LOC = self.items.get_loc(項目) 3291其他: 3292索引= np.arange(LEN(self.items)) [ISNULL(self.items)]
/opt/conda/lib/python3.5/site-packages/pandas/indexes/base.py in
get_loc(self, key, method, tolerance)
1945 return self._engine.get_loc(key)
1946 except KeyError:
-> 1947 return
self._engine.get_loc(self._maybe_cast_indexer(key))
1948
1949 indexer = self.get_indexer([key], method=method,
tolerance=tolerance)
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(pandas/hashtable.c:12368)()
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item
(pandas/hashtable.c:12322)()
KeyError: 0
感謝您回覆我。我試着添加你的建議。我在「更新的新版本」中有上面的新代碼,並且我收到了一組新的錯誤。我更熟悉編寫r代碼。所以我更喜歡適用於編寫循環,這就是爲什麼我試圖用apply來做到這一點。我仍然在python中得到這個竅門,所以非常感謝你的幫助。這些錯誤對你是否有意義,你能建議如何修復代碼嗎? – user3476463