0

I have a dataframe that where I am doing some currency computations. But when I rename a column in the standard way, I still get the same dataframe being displayed, despite me using the rename function

import pandas as pd
import matplotlib.pyplot as plt
import datetime

url1 = 'http://www.bankofcanada.ca/'
url2 = 'valet/observations/group/FX_RATES_DAILY/csv?start_date='

start_date = '2017-01-03'  
url = url1 + url2 + start_date  


rates = pd.read_csv(url, skiprows=39, index_col='date')
rates.index = pd.to_datetime(rates.index)  # assures data type to be a datetime

print("Print rates to the screen",rates)

#print index
print("Print index to the screen", rates.index)

days, currencies = rates.shape

codes = pd.read_csv(url, skiprows=10, usecols=[0,2],
                    nrows=currencies)

for i in range(currencies):
    codes.iloc[i, 1] = codes.iloc[i, 1].split(' to Canadian')[0]

#date = rates.index[-1]  

#Make a series of just the rates of FXAUDCAD
FXAUDCAD_daily = pd.DataFrame(rates['FXAUDCAD'])

#Print FXAUDRATES to the screen
print(FXAUDCAD_daily)

#Calculate the MA using the rolling function with a window size of 1
FXAUDCAD_daily['rolling mean'] = FXAUDCAD_daily['FXAUDCAD'].rolling(1).mean()

#print out the new dataframe with calculation
print(FXAUDCAD_daily)

#Rename one of the data frame from FXAUDCAD to Exchange Rate
FXAUDCAD_daily.rename(columns={'FXAUDCAD':'rate'})

#print out the new dataframe with calculation
print(FXAUDCAD_daily)

2 Answers2

0

The rename function returns the result, but you still have to update FXAUDCAD_daily like this:

FXAUDCAD_daily = FXAUDCAD_daily.rename(columns={'FXAUDCAD':'rate'})

Alternatively you can use the parameter inplace=True. If you want more information consider looking at this post.

PiaBa
  • 155
  • 5
  • 1
    FXAUDCAD_daily = FXAUDCAD_daily.rename(columns {'FXAUDCAD':'rate'}) . I originally used that, including inplace=True sorted it out. I didn't know it would just return a new overwritten dataframe with just that in it. – Kishan Bhatt Aug 12 '21 at 11:21
0
import pandas as pd
import matplotlib.pyplot as plt
import datetime

url1 = 'http://www.bankofcanada.ca/'
url2 = 'valet/observations/group/FX_RATES_DAILY/csv?start_date='

start_date = '2017-01-03'  
url = url1 + url2 + start_date  


rates = pd.read_csv(url, skiprows=39, index_col='date')
rates.index = pd.to_datetime(rates.index)  # assures data type to be a         
datetime

print("Print rates to the screen",rates)

#print index
print("Print index to the screen", rates.index)

days, currencies = rates.shape

codes = pd.read_csv(url, skiprows=10, usecols=[0,2],
                    nrows=currencies)

for i in range(currencies):
    codes.iloc[i, 1] = codes.iloc[i, 1].split(' to Canadian')[0]

#date = rates.index[-1]  

#Make a series of just the rates of FXAUDCAD
FXAUDCAD_daily = pd.DataFrame(rates['FXAUDCAD'])

#Print FXAUDRATES to the screen
print(FXAUDCAD_daily)

#Calculate the MA using the rolling function with a window size of 1
FXAUDCAD_daily['rolling mean'] =         
FXAUDCAD_daily['FXAUDCAD'].rolling(1).mean()

#print out the new dataframe with calculation
print(FXAUDCAD_daily)

#Rename one of the data frame from FXAUDCAD to Exchange Rate
FXAUDCAD_daily.rename(columns={'FXAUDCAD':'rate'}, inplace=True)

#print out the new dataframe with calculation
print(FXAUDCAD_daily)