I have data that contains fertility rates for different countries and I'd like to: 1. rename columns 2. Print out only specific countries (not using index but names)
Here I import data from website
df = pd.read_html('https://www.cia.gov/library/publications/the-world-factbook/fields/2127.html')
Then I try to rename columns (from '0' to 'Country' and from '1' to 'TFR'):
df= df.rename(index=str, columns ={'0':'Country', '1':'TFR'})
But I get error message:
df = df.rename(index=str, columns ={'0':'Country', '1':'TFR'})
AttributeError: 'list' object has no attribute 'rename'
This is the way in which I try to look for specific country:
print(df[df['0'].str.contains("Tanzan")])
And I get following error:
TypeError: list indices must be integers or slices, not str
What am I doing wrong? How to sort it out (if it is possible)? Thank you for your help!
fertilityRateByCountry.columns = ['Country', 'TFR']