Search code examples
pythonpandasdataframeyfinance

Changing column headers of dataframe


Probably a simple answer to this but Im stumped. So Im using yahoo to download the days prices of a list of company codes called "components". Components looks like this; (but with 91 entries).

components = ['ABEV3.SA', 'ALPA4.SA', 'AMER3.SA']

todays_data = yf.download(components, period= '1d')

From the resulting table I pulled the adjusted close and made a new dataframe:

close = todays_data['Adj Close']

Which results in a horizontal, 1 row, 91 column, dataframe called "close":

ABEV3.SA ALPA4.SA AMER3.SA
Date
2023-01-06 14.24 13.91 10.13

What Id like to get is a vertical dataframe, with the index column being the company codes and the column containing the price.

Ive used:

close.transpose()

which creates the vertical table the way Id like, 91 rows with 1 column, but the column headers are "Date" above the list of company codes (which Im guessing is the index), and "2023-01-06" above the prices.

Date 2023-01-06
ABEV3.SA 14.24
ALPA4.SA 13.91
AMER3.SA 10.13

How can I change "Date" and "2023-01-6" to, for example, "Code" and "Close"?

Ive tried variations of

close.rename()

such as

close.rename(columns= {'2023-01-06 00:00:00':'Close'})

but keep getting errors, like this? "IndexError: too many indices for array".

Perhaps something to do with the title of the value column (the prices) being a timestamp? [Timestamp('2023-01-06 00:00:00')]

Anyone help with this? Thanks...


Solution

  • You could do:

    df.rename_axis('Code').T.rename(columns={'2023-01-06':'Close'})