Pandas: inserting rows of even number years

£可爱£侵袭症+ 提交于 2019-12-24 16:09:35

问题


I have the following abridged dataframe:

df1 = pd.DataFrame({'end': [2007, 2013, 2014, 2013, 2014], 'id.thomas'\
: ['136', '136', '136', '172', '172'], 'years_exp': ['14', '20', '21', \
'14', '15']}, index=[2,3,4,5,6])

    end     id.thomas   years_exp
2   2007    136         14
3   2013    136         20
4   2014    136         21
5   2013    172         14
6   2014    172         15

where end is representative of years. I would like to expand the endand years_expcolumn to account account for the missing years:

    end     id.thomas   years_exp
2   2007    136         14
3   2008    136         15
4   2009    136         16
5   2010    136         17
6   2011    136         18
7   2012    136         19
8   2013    136         20
9   2014    136         21
10  2013    172         14
11  2014    172         15 

I have been working on this for about 20 hours, trying to 'engineer' a fix. Does anyone know of a simple Python/Pandas tool/method for accomplishing this task?


回答1:


This takes the first end and years_exp fields for a given id.thomas, and then enumerates these forward to the final year.

final_year = 2014
>>> pd.DataFrame([(year, id_, n) 
                  for id_, end, years_exp in df1.groupby('id.thomas').first().itertuples() 
                  for n, year in enumerate(range(end, final_year + 1), years_exp)], 
                 columns=['end', 'id.thomas', 'years_exp'])
    end  id.thomas  years_exp
0  2007        136         14
1  2008        136         15
2  2009        136         16
3  2010        136         17
4  2011        136         18
5  2012        136         19
6  2013        136         20
7  2014        136         21
8  2013        172         14
9  2014        172         15



回答2:


If years_exp doesn't yet matter, you can just build the dataframe from groupby :

df2 =pd.concat(
   [pd.DataFrame({'id.thomas':id,'end':range(s.min(),s.max()+1)})
                     for (id,s) in df1.groupby('id.thomas').end])

For

    end id.thomas
0  2007       136
1  2008       136
2  2009       136
3  2010       136
4  2011       136
5  2012       136
6  2013       136
7  2014       136
0  2013       172
1  2014       172


来源:https://stackoverflow.com/questions/36653009/pandas-inserting-rows-of-even-number-years

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!