Home > Enterprise >  Changing column headers of dataframe
Changing column headers of dataframe

Time:01-07

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...

CodePudding user response:

Date seems to be the name of the column index, which happens because it was the name of the row index before you transposed.

So we need to remove the column index name, give the row index a new name, and change the column index itself:

import pandas as pd

df = pd.DataFrame(
    [14.24, 13.91, 10.13],
    index=["ABEV3.SA", "ALPA4.SA", "AMER3.SA"],
    columns=pd.Index([pd.Timestamp("2023-01-06")], name="Date")
)
# Date      2023-01-06
# ABEV3.SA       14.24
# ALPA4.SA       13.91
# AMER3.SA       10.13

df = df.rename_axis(None, axis=1)
#           2023-01-06
# ABEV3.SA       14.24
# ALPA4.SA       13.91
# AMER3.SA       10.13

df = df.rename_axis("Code", axis=0)
#           2023-01-06
# Code
# ABEV3.SA       14.24
# ALPA4.SA       13.91
# AMER3.SA       10.13

df = df.rename(columns={pd.Timestamp("2023-01-06"): "Close"})
#           Close
# Code
# ABEV3.SA  14.24
# ALPA4.SA  13.91
# AMER3.SA  10.13

If you want, you can chain all these operations together

CodePudding user response:

You could do:

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