Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I created a for loop and generate an output which is a dataframe. However, I can't append the output to the input dataframe. Can you help to figure out what's wrong with the following codes?

Python
df0 = pd.DataFrame()
range1 = df_ssALL['a'].unique()
range2 = df_ssALL['b'].unique()
for i in range1:
    for j in range2:
        record = df_ssALL.loc[(df_ssALL['a']==i)&(df_ssALL['b']==j)]
        if len(record)<2:
            continue
        endog = record['feature1']
        exog = sm.add_constant(record['feature2'])
        rols = RollingOLS(endog, exog, window=2)
        rres = rols.fit()
        record['const'] = rres.params['const']
        record['slop'] = rres.params['feature2']
        df0.append(record, ignore_index=True)


What I have tried:

KeyError                                  Traceback (most recent call last)
/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2894             try:
-> 2895                 return self._engine.get_loc(casted_key)
   2896             except KeyError as err:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'const'
Posted
Comments
Richard MacCutchan 18-Aug-22 5:29am    
As far as I can tell from the error message, the key 'const' is not valid for that dataframe.
lauan 18-Aug-22 21:24pm    
Hmmm...I checked the dataframe (rres.params) The column names are correct.
<class 'pandas.core.frame.dataframe'="">
Int64Index: 7 entries, 0 to 52
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 const 6 non-null float64
1 feature2 6 non-null float64
dtypes: float64(2)
memory usage: 168.0 bytes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900