I'm trying to apply a form of normalization to the data I have. I wish to subtract the median of each row from each value in the dataframe. What I have so far:
# Generate sample data
data = { "sample_name": ["s1", "s2", "s3", "s4", "s5", "s6"],
"group_name": ["g1", "g1", "g1", "g2", "g2", "g2"],
'col1':[1, 22, 3, 45, 31, 53],
'col2':[30, 21, 10, 42, 56, 20],
'col3':[78, 25, 33, 87, 20, 19],
'col4':[11, 23, 14, 98, 55, 66],
'col5':[19, 29, 39, 49, 59, 69],
}
df = pd.DataFrame(data)
# calculate medians of each row
median_ls = list(df.median(axis=1))
# [19.0, 23.0, 14.0, 49.0, 55.0, 53.0]
The expected result is:
-18,11,59,-8,0
-1,-2,2,0,6
-11,-4,19,0,25
-4,-7,38,49,0
-24,1,-35,0,4
0,-33,-34,13,16
I've looked at df.apply(<function>, axis=1), but have been unable to work out the syntax of how to iteratively apply a row-specific function across rows.