python - Pandas backward fill increment by 12 months -
i have dataframe course names each year. need find duration in months starting year 2016.
from io import stringio import pandas pd u_cols = ['page_id','web_id'] audit_trail = stringio(''' year_id | web_id 2012|efg 2013|abc 2014| xyz 2015| pqr 2016| mnp ''') df11 = pd.read_csv(audit_trail, sep="|", names = u_cols )
how add months in new column starting highest (i.e. bottom bfill?)
the final data-frame this...
u_cols = ['page_id','web_id' , 'months'] audit_trail = stringio(''' year_id | web_id | months 2012|efg | 60 2013|abc | 48 2014| xyz | 36 2015| pqr | 24 2016| mnp | 12 ''') df12 = pd.read_csv(audit_trail, sep="|", names = u_cols )
some of answers not consider there can multiple courses. updating sample data...
from io import stringio import pandas pd u_cols = ['course_name','page_id','web_id'] audit_trail = stringio(''' course_name| year_id | web_id a|2012|efg a|2013|abc a|2014| xyz a|2015| pqr a|2016| mnp b|2014| xyz b|2015| pqr b|2016| mnp ''') df11 = pd.read_csv(audit_trail, sep="|", names = u_cols )
>>> df11.assign(months=df11.groupby('course_name').year_id.transform( lambda years: range(len(years) * 12, 0, -12))) course_name year_id web_id months 0 2012 efg 60 1 2013 abc 48 2 2014 xyz 36 3 2015 pqr 24 4 2016 mnp 12 5 b 2014 xyz 36 6 b 2015 pqr 24 7 b 2016 mnp 12
Comments
Post a Comment