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