0

I did a transpose of my dataframe and now I have headers that consist of numbers from 1 - 21384.

df_final.columns.values
Out[39]: array([    0,     1,     2, ..., 21382, 21383, 21384], dtype=int64)

                       0       1       2       3      ...   21381   21382  \
qyear                 2011Q1  2012Q1  2013Q1  2011Q1  ...  2013Q1  2011Q1   
RCFD3531                   0       0       0       0  ...       0       0   
RCFD3532                   0       0       0       0  ...       0       0   
RCFD3533                   0       0       0       0  ...       0       0   
RCFD3541                   0       0       0       0  ...       0       0   

Is there a way to eliminate these headers and replace them with row #1? Or, perhaps ignore them? Or maybe update them based on row #1? The actual headers that I want to use are in the row named 'qyear':

qyear                 2011Q1  2012Q1  2013Q1  2011Q1  ...  2013Q1  2011Q1

Basically, I want to do a merge, and it's not working.

df_final = pd.merge(df_final, df_list, left_on='qyear', right_on='IDRSSD_ID', how='inner')

Traceback (most recent call last):

  File "<ipython-input-42-324d32761a8c>", line 1, in <module>
    df_final = pd.merge(df_final, df_list, left_on='qyear', right_on='IDRSSD_ID', how='inner')

  File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 87, in merge
    validate=validate,

  File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 652, in __init__
    ) = self._get_merge_keys()

  File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 1018, in _get_merge_keys
    left_keys.append(left._get_label_or_level_values(lk))

  File "C:\Users\ryans\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1563, in _get_label_or_level_values
    raise KeyError(key)

KeyError: 'qyear'
ASH
  • 20,759
  • 19
  • 87
  • 200
  • That does fix the header problem!! Sweet!! However, I still can't do the merge. I'm getting the same error as before: KeyError: 'qyear' – ASH Jan 20 '21 at 02:09
  • 1
    Yeah `qyear` is your index - it's not a column. You can either use `reset_index` or you can look at merging on index, there are examples of how to do that in the documentation. – Chris Jan 20 '21 at 02:12
  • 1
    Got everything working now! Thanks for the point in the right direction here! Really appreciate it!! – ASH Jan 20 '21 at 03:20

0 Answers0