Home > database >  Renaming a column name works occasionally in Python
Renaming a column name works occasionally in Python

Time:09-14

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.

CodePudding user response:

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']

CodePudding user response:

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.

  • Related