简体   繁体   English

如何提取一系列年份并在给定一对日期的情况下生成新行

[英]How to extract a range of years and generate new rows given a pair of dates

I have two data frames that I would eventually like to merge in order to compare differences between alternative spellings of leader names.我有两个数据框,我最终想要合并它们,以便比较领导者姓名的替代拼写之间的差异。

My first dataframe looks something like the following:我的第一个数据框如下所示:

year    country_isocode country_name    leader  leader_start_date   leader_end_date
20  1986    AFG Afghanistan Mohammed Najibullah 1986-05-04  1992-04-16
21  1987    AFG Afghanistan Mohammed Najibullah 1986-05-04  1992-04-16
22  1988    AFG Afghanistan Mohammed Najibullah 1986-05-04  1992-04-16
23  1989    AFG Afghanistan Mohammed Najibullah 1986-05-04  1992-04-16
24  1990    AFG Afghanistan Mohammed Najibullah 1986-05-04  1992-04-16
25  1991    AFG Afghanistan Mohammed Najibullah 1986-05-04  1992-04-16
26  1992    AFG Afghanistan Burhanuddin Rabbani 1992-06-28  1996-09-27
27  1993    AFG Afghanistan Burhanuddin Rabbani 1992-06-28  1996-09-27
28  1994    AFG Afghanistan Burhanuddin Rabbani 1992-06-28  1996-09-27

While my second looks like this:虽然我的第二个看起来像这样:

leader_start_date   leader_end_date LeaderCountryOrIGO  LeaderCountryISO    LeaderTitle LeaderLastName  LeaderFullName
0   1986-05-04  1990-06-28  Afghanistan AFG General Secretary   Najibullah  Mohammad Najibullah
1   1989-02-21  1990-05-07  Afghanistan AFG Prime Minister  Keshtmand   Ali Keshtmand
2   1990-05-07  1992-04-15  Afghanistan AFG Prime Minister  Khaliqyar   Fazal Haq Khaliqyar
3   1992-04-16  1992-04-28  Afghanistan AFG President (Acting)  Hatef   Abdul Rahim Hatef
4   1992-04-28  1992-06-28  Afghanistan AFG President (Acting)  Mojadedi    Sibghatullah Mojadedi
5   1992-06-28  1996-09-27  Afghanistan AFG President   Rabbani Burhanuddin Rabbani

The first data frame has individual rows for every country-year entry in the dataset while the second data frame has a single row for a unique leader with their range of years in office.第一个数据框为数据集中的每个国家/地区年条目提供单独的行,而第二个数据框为唯一的领导者及其在任年限的单行。 My goal is to re-shape the second data frame so that it is comparable to the shape of the first.我的目标是重新调整第二个数据框的形状,使其与第一个的形状相当。

I would like to take the year range implied by the two datetime columns "leader_start_date, "leader_end_date" in the second data set and "expand" these to create sets of new rows for every year in that range which include duplicate information regarding country and leader name. I would then need to iterate this solution for all unique leader names and their year ranges in the second date frame.我想采用第二个数据集中的两个日期时间列“leader_start_date”和“leader_end_date”隐含的年份范围,并“扩展”它们以在该范围内为该范围内的每一年创建新行集,其中包括有关国家和领导人的重复信息名称。然后我需要在第二个日期框架中为所有唯一的领导者名称及其年份范围迭代此解决方案。

While the data sets aren't a perfect match, getting both data frames in the same shape will allow me to identify a number of matches.虽然数据集不是完美匹配,但让两个数据框具有相同的形状将使我能够识别多个匹配项。

Use:用:

#convert both columns to datetimes
df['leader_start_date'] = pd.to_datetime(df['leader_start_date'])
df['leader_end_date'] = pd.to_datetime(df['leader_end_date'])

#create new column by years
df.insert(0, 'year', df['leader_start_date'].dt.year)

#subtract years for repeating, repalce missing values by actual year
s = df['leader_end_date'].dt.year.fillna(pd.to_datetime('now').year) - df['year']

#if output is previous year by leader_end_date
df = df.loc[df.index.repeat(s)].copy()
#if output match also year in leader_end_date
# df = df.loc[df.index.repeat(s + 1)].copy()

#add counter to column year
df['year'] += df.groupby(level=0).cumcount()

#create default index
df = df.reset_index(drop=True)

print (df)
    year leader_start_date leader_end_date LeaderCountryOrIGO  \
0   1986        1986-05-04      1990-06-28        Afghanistan   
1   1987        1986-05-04      1990-06-28        Afghanistan   
2   1988        1986-05-04      1990-06-28        Afghanistan   
3   1989        1986-05-04      1990-06-28        Afghanistan   
4   1989        1989-02-21      1990-05-07        Afghanistan   
5   1990        1990-05-07      1992-04-15        Afghanistan   
6   1991        1990-05-07      1992-04-15        Afghanistan   
7   1992        1992-06-28      1996-09-27        Afghanistan   
8   1993        1992-06-28      1996-09-27        Afghanistan   
9   1994        1992-06-28      1996-09-27        Afghanistan   
10  1995        1992-06-28      1996-09-27        Afghanistan   

   LeaderCountryISO        LeaderTitle LeaderLastName       LeaderFullName  
0               AFG  General Secretary     Najibullah  Mohammad Najibullah  
1               AFG  General Secretary     Najibullah  Mohammad Najibullah  
2               AFG  General Secretary     Najibullah  Mohammad Najibullah  
3               AFG  General Secretary     Najibullah  Mohammad Najibullah  
4               AFG     Prime Minister      Keshtmand        Ali Keshtmand  
5               AFG     Prime Minister      Khaliqyar  Fazal Haq Khaliqyar  
6               AFG     Prime Minister      Khaliqyar  Fazal Haq Khaliqyar  
7               AFG          President        Rabbani  Burhanuddin Rabbani  
8               AFG          President        Rabbani  Burhanuddin Rabbani  
9               AFG          President        Rabbani  Burhanuddin Rabbani  
10              AFG          President        Rabbani  Burhanuddin Rabbani  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM