简体   繁体   中英

percentile of the last value of a row

I am trying to get the percentile value for the last value in each row and store it in a different column. But unable to (new to python). What i have been able to achieve is the percentile value of each row through indexing. But not my desired output.

Following the code:

df = pd.DataFrame(np.random.randint(20,60,size=(10, 7)), columns=list('ABCDEFG'))

values = df.loc[1][0:]
min_value = values.min()
max_value = values.max()

percentiles = ((values - min_value) / (max_value - min_value) * 100)

print(percentiles)

current output:

    A   B   C   D
0  35  45  25  38
2  35  31  28  55
3  59  38  44  40
4  40  57  30  52
5  20  51  31  48
6  52  24  39  49
7  47  59  39  47
8  20  42  21  26
9  27  53  38  56

I am getting this way the percentile value:

A     61.538462
B     65.384615
C    100.000000
D     61.538462
E     50.000000
F     96.153846
G      0.000000

desired output:

    A   B   C   D   E   F   G  Per
0  52  41  23  53  22  22  39  23.6
1  48  49  58  48  45  57  32  23.5
2  38  49  48  25  32  22  27  56.2
3  46  34  43  52  50  32  30  63.5
4  59  47  49  22  53  31  38  65.9
5  49  49  58  37  28  31  34  50.2
6  31  29  28  41  39  36  47  90.2
7  34  55  52  39  32  25  55  85.6
8  34  21  48  22  22  53  42  80.5
9  44  23  57  52  29  54  43  90.6

Per value is the percentile value of col G for each row when compared to that row.

Try:

def perc_func(r):
    x = r
    last_val = x[-1]
    min_val = x.min()
    max_val = x.max()
    percentile = ((last_val - min_val) / (max_val - min_val) * 100)  
    return percentile

df['Per'] = df.apply(lambda row:perc_func(row), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM