Eko123
Eko123

Reputation: 3

Pandas Dataframe - Renaming of last column doesn't work

I am having issues with renaming last column of my dataframe. My dataframe has 47 columns and I am able to rename all columns except the last one.

data.rename(
{
    "Electricity:Zone:B001 [J](TimeStep)": "strom_verbr_zone",
    "Electricity:Facility [J](TimeStep)": "strom_verbr_ges",
    "B001FEOST:Surface Shading Device Is On Time Fraction [](TimeStep)": "f_verschattung_io",
    "Gas:Facility [J](TimeStep)": "gas_verbr"
}, axis=1, inplace=True)

After running this Code every column Name changes except the last one. The column "Gas:Facility J" just won't rename. Tried it on other ways as well but without any success until now. Does anyone have an idea how to make it work or why it doesn't rename?

Upvotes: 0

Views: 1331

Answers (2)

matman9
matman9

Reputation: 490

could try:

cols = data.columns 

cols[-1] = 'new name'

data.columns = cols

Upvotes: 0

Anshul
Anshul

Reputation: 1413

Simply try:

# in the same order as you need
data.columns = ["strom_verbr_zone", "strom_verbr_ges", "f_verschattung_io", "gas_verbr"]

Testing renaming the last column in an empty dataframe with the names as provided:

# created a blank dataframe with just the headers with given column names
df = pd.DataFrame(columns=["Electricity:Zone:B001 [J](TimeStep)", 
"Electricity:Facility [J](TimeStep)", 
"B001FEOST:Surface Shading Device Is On Time Fraction [](TimeStep)",
"Gas:Facility [J](TimeStep)" ])

print(df.columns)

Output:

Index(['Electricity:Zone:B001 [J](TimeStep)',
       'Electricity:Facility [J](TimeStep)',
       'B001FEOST:Surface Shading Device Is On Time Fraction [](TimeStep)',
       'Gas:Facility [J](TimeStep)'],
      dtype='object')

.

# renamed just the last column
df.rename(
{
    "Gas:Facility [J](TimeStep)": "gas_verbr"
}, axis=1, inplace=True)

print(df.columns)

Output after renaming just the last column:

Index(['Electricity:Zone:B001 [J](TimeStep)',
       'Electricity:Facility [J](TimeStep)',
       'B001FEOST:Surface Shading Device Is On Time Fraction [](TimeStep)',
       'gas_verbr'],
      dtype='object')

Upvotes: 1

Related Questions