python - Divide part of a dataframe by another while keeping columns that are not being divided -
i have 2 data frames below:
sample_name c14-cer c16-cer c18-cer c18:1-cer c20-cer 0 1 1 0.161456 0.033139 0.991840 2.111023 0.846197 1 1 10 0.636140 1.024235 36.333741 16.074662 3.142135 2 1 13 0.605840 0.034337 2.085061 2.125908 0.069698 3 1 14 0.038481 0.152382 4.608259 4.960007 0.162162 4 1 5 0.035628 0.087637 1.397457 0.768467 0.052605 5 1 6 0.114375 0.020196 0.220193 7.662065 0.077727 sample_name c14-cer c16-cer c18-cer c18:1-cer c20-cer 0 1 1 0.305224 0.542488 66.428382 73.615079 10.342252 1 1 10 0.814696 1.246165 73.802644 58.064363 11.179206 2 1 13 0.556437 0.517383 50.555948 51.913547 9.412299 3 1 14 0.314058 1.148754 56.165767 61.261950 9.142128 4 1 5 0.499129 0.460813 40.182454 41.770906 8.263437 5 1 6 0.300203 0.784065 47.359506 52.841821 9.833513
i want divide numerical values in selected cells of first second , using following code:
df1_int.loc[:,'c14-cer':].div(df2.loc[:,'c14-cer':])
however, way lose information column "sample_name".
c14-cer c16-cer c18-cer c18:1-cer c20-cer 0 0.528977 0.061088 0.014931 0.028677 0.081819 1 0.780831 0.821909 0.492309 0.276842 0.281070 2 1.088785 0.066367 0.041243 0.040951 0.007405 3 0.122529 0.132650 0.082047 0.080964 0.017738 4 0.071381 0.190178 0.034778 0.018397 0.006366 5 0.380993 0.025759 0.004649 0.145000 0.007904
how can perform division while keeping column "sample_name" in resulting dataframe?
you can selectively overwrite using loc
, same way you're performing division:
df1_int.loc[:,'c14-cer':] = df1_int.loc[:,'c14-cer':].div(df2.loc[:,'c14-cer':])
this preserves sample_name col:
in [12]: df.loc[:,'c14-cer':] = df.loc[:,'c14-cer':].div(df1.loc[:,'c14-cer':]) df out[12]: sample_name c14-cer c16-cer c18-cer c18:1-cer c20-cer index 0 1 1 0.528975 0.061087 0.014931 0.028677 0.081819 1 1 10 0.780831 0.821910 0.492309 0.276842 0.281070 2 1 13 1.088785 0.066367 0.041243 0.040951 0.007405 3 1 14 0.122528 0.132650 0.082047 0.080964 0.017738 4 1 5 0.071380 0.190179 0.034778 0.018397 0.006366 5 1 6 0.380992 0.025758 0.004649 0.145000 0.007904
Comments
Post a Comment