Renaming Column Not Working even Though Syntax is Correct in Python

Question:

I have been trying to rename the column name in a csv file which I have been working on through Google-Colab. But the same line of code is working on one column name and is also not working for the other.

import pandas as pd
import numpy as np
data = pd.read_csv("Daily Bike Sharing.csv",
                   index_col="dteday",
                   parse_dates=True)
dataset = data.loc[:,["cnt","holiday","workingday","weathersit",
                      "temp","atemp","hum","windspeed"]]
dataset = dataset.rename(columns={'cnt' : 'y'})
dataset = dataset.rename(columns={"dteday" : 'ds'})
dataset.head(1)

The Image below is the dataframe called data
enter image description here

The Image below is dataset
enter image description here

This image is the final output which I get when I try to rename the dataframe.
enter image description here

The column name "dtedate" is not getting renamed but "cnt" is getting replaced "y" by the same code. Can someone help me out, I have been racking my brain on this for sometime now.

Asked By: Mr Pool

||

Answers:

That’s because you’re setting dteday as your index, upon reading in the csv, whereas cnt is quite simply a column. Avoid the index_col attribute in read_csv and instead perform dataset = dataset.set_index(‘ds’) after renaming.

An alternative in which only your penultimate line (trying to rename the index) would need to be changed:

dataset.index.names = ['ds']
Answered By: abbasador

You can remove the ‘index-col’ in the read statement, include ‘dtedate’ in your dataset and then change the column name. You can make the column index using df.set_index later.

Answered By: naveen kumar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.