I am borrowing this example from here. I have a dataframe like this:
# Import pandas package
import pandas as pd
# Define a dictionary containing ICC rankings
rankings = {'test': ['India', 'South Africa', 'England',
'New Zealand', 'Australia'],
'odi': ['England', 'India', 'New Zealand',
'South Africa', 'Pakistan'],
't20': ['Pakistan', 'India', 'Australia',
'England', 'New Zealand']}
# Convert the dictionary into DataFrame
rankings_pd = pd.DataFrame(rankings)
# Before renaming the columns
print(rankings_pd)
test odi t20
0 India England Pakistan
1 South Africa India India
2 England New Zealand Australia
3 New Zealand South Africa England
4 Australia Pakistan New Zealand
Now let's say I want to change the name of the 1st and 2nd columns. This is what I am trying:
rankings_pd[rankings_pd.columns[0:2]].columns = ['tes_after_change', 'odi_after_change']
print(rankings_pd[rankings_pd.columns[0:2]].columns)
Index(['test', 'odi'], dtype='object')
But this seems to return exactly the same columns names and not changing them.