[英]How to print data for a specific value (string) from a data frame (pandas)
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) 我有包含不同国家生育率的数据,我想:1.重命名列2.仅打印特定国家(不使用索引,而是使用名称)
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'): 然后,我尝试重命名列(从“ 0”到“国家”,从“ 1”到“ 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!
谢谢您的帮助!
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: 首先添加参数
header=0
以将页面的第一行转换为DataFrame的标题,然后添加[0]
以从DataFrames列表中选择第一个DataFrame:
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.)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.