Merging Two Rows Into One Longer Row (Pandas) -


i have data set 1048 rows, each being 2 sides of same data set (a democrat , republican campaign in same race).

year label can_par_aff can_inc_cha_ope_sea dem_per gop_per ind_con 0 2014 ak-1 dem 1 40.8 51.6 36500.64
1 2014 ak-1 rep 0 40.8 51.6 43369.00
, need merge them single row ind_con_dem , ind_con_gop (so can create comparison metrics).

i cannot seem find simple way automate this. important factors groups both 'year' , 'label' , creates new column names.

i can specific code, cannot figure out way generalize it.

thoughts, ideas, etc?

assuming dataframe df , label , year uniquely identify records, try:

df_dem = df[df['can_par_aff'] == 'dem'].groupby(['year','label']).last() df_rep = df[df['can_par_aff'] == 'rep'].groupby(['year','label']).last()  joined = df_dem.join(df_rep, lsuffix = '_dem', rsuffix = '_gop') 

it's grouped year , label , creates new names specified above.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -