Calculate difference from a reference row in pandas (python) -
in pandas have data frame of type:
value samplegroup sample group1 ref 18.1 smp1 nan smp2 20.3 smp3 30.0 smp4 23.8 smp5 23.2 what want do add new column reference (ref) has been subtracted samples (smp). this:
value deltavalue samplegroup sample group1 ref 18.1 0 smp1 nan nan smp2 20.3 2.2 smp3 30.0 11.9 smp4 23.8 5.7 smp5 23.2 5.1 does know how can done? thanks!
ok knocked following worked me:
in [327]: t="""sample value ref 18.1 smp1 nan smp2 20.3 smp3 30.0 smp4 23.8 smp5 23.2""" df = pd.read_csv(io.stringio(t), sep='\s+') df out[327]: sample value 0 ref 18.1 1 smp1 nan 2 smp2 20.3 3 smp3 30.0 4 smp4 23.8 5 smp5 23.2 in [328]: df['group'] = 'group1' df out[328]: sample value group 0 ref 18.1 group1 1 smp1 nan group1 2 smp2 20.3 group1 3 smp3 30.0 group1 4 smp4 23.8 group1 5 smp5 23.2 group1 in [329]: df1 = df.set_index(['group', 'sample']) df1 out[329]: value group sample group1 ref 18.1 smp1 nan smp2 20.3 smp3 30.0 smp4 23.8 smp5 23.2 in [337]: df1['deltavalue'] = df1['value'].sub(df1.loc[('group1','ref')]['value']) df1 out[337]: value deltavalue group sample group1 ref 18.1 0.0 smp1 nan nan smp2 20.3 2.2 smp3 30.0 11.9 smp4 23.8 5.7 smp5 23.2 5.1 also following worked:
df1['deltavalue'] = df1['value'] - df1.loc[('group1','ref')]['value']
Comments
Post a Comment