【发布时间】:2021-02-20 07:41:09
【问题描述】:
我需要为每个国家/地区生成不同年份的人口列表。我需要的信息包含在两个数据框中
-
第一个数据框 gni_per_capita 包含国家名称和 年。此数据框中的国家/地区具有不同的年份范围
-
第二个数据框 hihd 也有国家和日期的名称, 但国家名单更广泛,范围更广 每个国家/地区的日期范围。 第二个数据框包含每个国家每年的人口,第二个没有。
我需要为第一个数据框中的每个国家/地区每年生成一份人口列表。
我得到了以下提示:
1. first, get a unique list of countries from gni_per_capita.
2. Loop through the list, and get the available years for that country.
3. Then .loc index hihd to get the population rows where both the country
and years are correct (hihd.Year.isin(?)).
4. Append these to the list
one by one.
到目前为止,我已经从第一个数据框创建了一个包含国家和年份的系列
group = gni_per_capita.groupby('Entity')
ync = group.apply(lambda x: x['Year'].unique())
但是,我正在努力将第二个数据框与创建的系列结合起来
mask = hihd.Year.isin(ync)
【问题讨论】:
-
这里有一些很好的例子来帮助人们回答你的问题:stackoverflow.com/questions/20109391/…
标签: python pandas dataframe lambda isin