How to print data for a specific value (string) from a data frame (pandas)

ugabuga77

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!

jezrael

First add parameter header=0 for convert first row of page to header of DataFrame and then add [0] for select first DataFrame from list of DataFrames:

url = 'https://www.cia.gov/library/publications/the-world-factbook/fields/2127.html'
d = {'TOTAL FERTILITY RATE(CHILDREN BORN/WOMAN)':'TFR'}
df = pd.read_html(url, header=0)[0].rename(columns=d)
print (df.head())
          Country                                   TFR
0     Afghanistan  5.12 children born/woman (2017 est.)
1         Albania  1.51 children born/woman (2017 est.)
2         Algeria   2.7 children born/woman (2017 est.)
3  American Samoa  2.68 children born/woman (2017 est.)
4         Andorra   1.4 children born/woman (2017 est.)

Last filter by new column name:

print(df[df['Country'].str.contains("Tanzan")])
      Country                                   TFR
204  Tanzania  4.77 children born/woman (2017 est.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get a single value as a string from pandas data frame

How to check if a value is in the list in selection from pandas data frame?

How to check if a value is in the list in selection from pandas data frame?

Extract date from string Pandas data frame

selecting a specific value from a data frame

How to print occurence of given string on pandas data frame column?

Pandas - Data Frame string value count

How to find a specific value from a column in Data Frame in Python using pandas

How to get the index of the mode value of a specific column in a pandas data frame

Selecting specific rows from a pandas data frame

Extract integers from string value in a pandas data frame cell

How to create a dummy column from a string expression in pandas data frame?

How to delete fraction of rows that has specific attribute value from pandas data frame

R-How to get specific string value in data frame header

How to look for a value in a specific pandas data frame column and then store the other values from that row in separate variables

How to extract a value from a Pandas data frame from a reference in the frame, then "walk up" the frame to another specified value?

How to import specific values from a data frame into a variable in pandas

How to extract entire rows from pandas data frame, if a column's string value contains a specific pattern

How to get cell value from pandas data frame

How to get the whole row from data frame having maximum value for every 7 records in the pandas data frame?

how do create a pandas data frame from a string output in python

Add new column to pandas data frame based on string + value from another column in the data frame

How to print specific data based on its rank in data frame

How to correctly extract substring from a string in a Pandas data frame?

How to obtain the weight of a specific value in a data frame?

Removing index from pandas data frame on print

Extract a value from a JSON string stored in a pandas data frame column

How to replace specific string in data frame column value?

How to select specific values in a pandas data frame?