Python Pandas:将特定函数应用于每一行

我正在尝试对我拥有的数据应用一种规范化形式。我希望从数据框中的每个值中减去每行的中值。到目前为止我所拥有的:


# 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]

预期结果是:


-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

我看过df.apply(<function>, axis=1),但无法弄清楚如何跨行迭代应用特定于行的函数的语法。


呼如林
浏览 129回答 3
3回答

慕少森

用于DataFrame.select_dtypes获取数字列并减去DataFrame.subwith axis=1:df1 = df.select_dtypes(np.number).sub(df.median(axis=1), axis=0)print (df1)   col1  col2  col3  col4  col50 -18.0  11.0  59.0  -8.0   0.01  -1.0  -2.0   2.0   0.0   6.02 -11.0  -4.0  19.0   0.0  25.03  -4.0  -7.0  38.0  49.0   0.04 -24.0   1.0 -35.0   0.0   4.05   0.0 -33.0 -34.0  13.0  16.0如果需要分配回输出使用:cols = df.select_dtypes(np.number).columnsdf[cols] = df[cols].sub(df.median(axis=1), axis=0)print (df)  sample_name group_name  col1  col2  col3  col4  col50          s1         g1 -18.0  11.0  59.0  -8.0   0.01          s2         g1  -1.0  -2.0   2.0   0.0   6.02          s3         g1 -11.0  -4.0  19.0   0.0  25.03          s4         g2  -4.0  -7.0  38.0  49.0   0.04          s5         g2 -24.0   1.0 -35.0   0.0   4.05          s6         g2   0.0 -33.0 -34.0  13.0  16.0另一个想法是通过以下方式选择没有前 2 行的所有行DataFrame.iloc:df.iloc[:, 2:] = df.iloc[:, 2:].sub(df.median(axis=1), axis=0)print (df)  sample_name group_name  col1  col2  col3  col4  col50          s1         g1 -18.0  11.0  59.0  -8.0   0.01          s2         g1  -1.0  -2.0   2.0   0.0   6.02          s3         g1 -11.0  -4.0  19.0   0.0  25.03          s4         g2  -4.0  -7.0  38.0  49.0   0.04          s5         g2 -24.0   1.0 -35.0   0.0   4.05          s6         g2   0.0 -33.0 -34.0  13.0  16.0

SMILET

尝试:df.sub(df.median(axis=1),&nbsp;axis=0)

慕斯709654

我只允许自己使用数字部分import pandas as pd# Generate sample datadata = {&nbsp; &nbsp; "sample_name": ["s1", "s2", "s3", "s4", "s5", "s6"],&nbsp; &nbsp; "group_name": ["g1", "g1", "g1", "g2", "g2", "g2"],&nbsp; &nbsp; 'col1':[1, 22, 3, 45, 31, 53],&nbsp; &nbsp; 'col2':[30, 21, 10, 42, 56, 20],&nbsp; &nbsp; 'col3':[78, 25, 33, 87, 20, 19],&nbsp; &nbsp; 'col4':[11, 23, 14, 98, 55, 66],&nbsp; &nbsp; 'col5':[19, 29, 39, 49, 59, 69],&nbsp; &nbsp;}keys = ['col1','col2','col3','col4','col5']df = pd.DataFrame(data)print(df)# calculate medians of each rowmedian_ls = list(df.median(axis=1))# [19.0, 23.0, 14.0, 49.0, 55.0, 53.0]print(median_ls)print(df[keys].subtract(median_ls, axis=0))结果:&nbsp; &nbsp;col1&nbsp; col2&nbsp; col3&nbsp; col4&nbsp; col50 -18.0&nbsp; 11.0&nbsp; 59.0&nbsp; -8.0&nbsp; &nbsp;0.01&nbsp; -1.0&nbsp; -2.0&nbsp; &nbsp;2.0&nbsp; &nbsp;0.0&nbsp; &nbsp;6.02 -11.0&nbsp; -4.0&nbsp; 19.0&nbsp; &nbsp;0.0&nbsp; 25.03&nbsp; -4.0&nbsp; -7.0&nbsp; 38.0&nbsp; 49.0&nbsp; &nbsp;0.04 -24.0&nbsp; &nbsp;1.0 -35.0&nbsp; &nbsp;0.0&nbsp; &nbsp;4.05&nbsp; &nbsp;0.0 -33.0 -34.0&nbsp; 13.0&nbsp; 16.0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python