简体   繁体   English

如何从数据框(熊猫)中打印特定值(字符串)的数据

[英]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.

相关问题 从 Pandas 数据框的字符串列中提取特定值 - Extract specific value from the string column of a Pandas Data frame 如果列的字符串值包含特定模式,如何从 pandas 数据帧中提取整行 - How to extract entire rows from pandas data frame, if a column's string value contains a specific pattern 如何从python pandas数据框中提取特定值? - How to pull out a specific value from a python pandas data frame? 如何从熊猫数据框中获取单个值作为字符串 - How to get a single value as a string from pandas data frame 如何在pandas数据框列上打印给定字符串的出现? - How to print occurence of given string on pandas data frame column? 在 Pandas 数据框中检索字符串的特定部分 - Retrieving a Specific part of string in a Pandas Data frame 在打印时从 pandas 数据帧中删除索引 - Removing index from pandas data frame on print 如何从 pandas 数据帧中的逗号分隔值计算以特定 substring 开头的字符串的出现次数? - How to count the occurrences of a string starts with a specific substring from comma separated values in a pandas data frame? 从存储在 pandas 数据框列中的 JSON 字符串中提取一个值 - Extract a value from a JSON string stored in a pandas data frame column 如何在 Pyscripter 中为 pandas 数据帧打印表格? - how to print a table for a pandas data frame in Pyscripter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM