数据源: https://www.kaggle.com/worldbank/world-development-indicators 文件夹:‘world-development-’File:Indicators.csv
我试图在两个变量之间绘制散点图。然而,这两个变量的大小并不相同。
数据库如下所示:它是通过名称数据保存的:
CountryCode IndicatorName Year Value
USA Population, total 1993 72498
USA Population, total 1994 76700
USA Population, female (% of total) 1993 50.52691109
USA Population, female (% of total) 1994 50.57235984
USA GDP per capita (const 2005 US$) 1994 23086.93795
USA Population, female (% of total) 1988 50.91933134
USA Population, total 1988 61077我想在两件事之间画一张散点图:绝对女性人口和人均GDP (const 2005美元)。绝对女性人口=人口,*总人口,女性(%)
挑战如下:
a)一个国家的总人口、女性人口和国内生产总值存在不同的年份。例如,美国,比方说人口的数值,总数只存在20年,女性人口的数字是18年,国内生产总值的数值只有10年。
没有NAN/Null值
I需要这些值,其中所有这些参数的值在给定年份的国家中都是。
我对python很陌生,所以我无法在代码中列出我想要的内容。有谁能帮忙吗?
femalepop_filter = data['IndicatorName'].str.contains('Population,
female')
FemalePop = data[femalepop_filter]
Pop_total=data['IndicatorName'].str.contains('Population, total')
Pop_Tot=data[Pop_total]
hist_indicator = 'GDP per capita \(const 2005'
GDP_Filter = data['IndicatorName'].str.contains(hist_indicator)
GDPValues=data[GDP_Filter]
c1 = (FemalePop['CountryCode'])
c2 = (GDPValues['CountryCode'])
c3 = (Pop_Tot['CountryCode'])
c4 = np.intersect1d(c1,c2)
c5 = np.intersect1d(c3,c4)我捕获了所有参数的国家代码。现在我在c5找到了他们的交叉口。有人能帮助我如何获得国家代码在c5中的数据吗?
发布于 2019-08-07 08:15:12
我找到了答案。
data2=data[data['CountryCode'].isin(c5)]
#Getting all the intersection of country codes in one dataset
data2['concatyearandCC'] = data2["CountryCode"] + "" + data2["Year"].map(str)
#Introducing new column which is concatenation of country code and Year so that I
#get all the rows corresponding to same year and country code.
c9 = pd.merge(FemalePop2,Pop_Tot2,on="concatyearandCC")
c10= pd.merge(c9,GDPValues2,on="concatyearandCC")
#Merging datasets containing female population%, GDP and total population of
#females so that I can calculate absolute number of females.
c10.rename(columns={'Value_x': 'Population_female%', 'Value_y': 'Population
Total', 'Value': 'GDP Per capita'}, inplace=True)
#Renaming some columns for ease.
c10_Final['Abs_Female_Pop'] = c10_Final['Population_female%']
*c10_Final['Population Total']
#Finding absolute female population发布于 2019-08-05 17:14:05
错误告诉您Python不知道如何连接("&")字符串和布尔变量。
将bool转换为字符串,您的连接应该可以工作。
通常,逐步调试代码。首先看看变量包含了什么。您可以使用Python的“漂亮打印”(pprint)模块。这样您就可以打印出各种变量,以查看它们包含了什么。
https://stackoverflow.com/questions/57361987
复制相似问题