将百分位数计算为熊猫中的列

时间:2019-01-31 20:36:01

标签: python pandas

我有以下数据框。我正在尝试计算“新近度”字段的百分位数,并将其添加为自己的字段。我一直在尝试在Pandas中使用分位数功能,但是得到的NaN输出如下所示。

有人可以建议我该怎么做吗?

       id  recency  frequency  monetary  recencypct
0       1       61         61   6052.50         NaN
1       2       43         97   1743.79         NaN
2       3       71         55   7293.29         NaN
3       4       32         77   4493.31         NaN
4       5        7         14   2036.86         NaN
5       6       57         41   1380.94         NaN
6       7       12         47   9451.65         NaN
7       8       98         12   8687.91         NaN
8       9       24         90   6350.54         NaN
9      10       41          8    599.80         NaN
10     11       61         17    212.13         NaN
11     12       29         89   8501.65         NaN
12     13        9         27   7165.10         NaN
13     14       77         31   6011.45         NaN
14     15       37          8   9491.75         NaN
15     16      100         76   1894.23         NaN
16     17       25          8   5753.13         NaN
17     18       19         45    333.16         NaN
18     19       14         90   8762.78         NaN
19     20       16         20    231.76         NaN

1 个答案:

答案 0 :(得分:3)

如果数据帧称为df,请尝试:

df['recencypct'] = df.recency.rank(pct=True)

输出(打印精美):

+----+------+-----------+-------------+------------+--------------+
|    |   id |   recency |   frequency |   monetary |   recencypct |
|----+------+-----------+-------------+------------+--------------|
|  0 |    1 |        61 |          61 |    6052.5  |        0.775 |
|  1 |    2 |        43 |          97 |    1743.79 |        0.65  |
|  2 |    3 |        71 |          55 |    7293.29 |        0.85  |
|  3 |    4 |        32 |          77 |    4493.31 |        0.5   |
|  4 |    5 |         7 |          14 |    2036.86 |        0.05  |
|  5 |    6 |        57 |          41 |    1380.94 |        0.7   |
|  6 |    7 |        12 |          47 |    9451.65 |        0.15  |
|  7 |    8 |        98 |          12 |    8687.91 |        0.95  |
|  8 |    9 |        24 |          90 |    6350.54 |        0.35  |
|  9 |   10 |        41 |           8 |     599.8  |        0.6   |
| 10 |   11 |        61 |          17 |     212.13 |        0.775 |
| 11 |   12 |        29 |          89 |    8501.65 |        0.45  |
| 12 |   13 |         9 |          27 |    7165.1  |        0.1   |
| 13 |   14 |        77 |          31 |    6011.45 |        0.9   |
| 14 |   15 |        37 |           8 |    9491.75 |        0.55  |
| 15 |   16 |       100 |          76 |    1894.23 |        1     |
| 16 |   17 |        25 |           8 |    5753.13 |        0.4   |
| 17 |   18 |        19 |          45 |     333.16 |        0.3   |
| 18 |   19 |        14 |          90 |    8762.78 |        0.2   |
| 19 |   20 |        16 |          20 |     231.76 |        0.25  |
+----+------+-----------+-------------+------------+--------------+

参考:http://www.datasciencemadesimple.com/percentile-rank-column-pandas-python-2/