如何返回每个类别的最大值?有些具有NAN值,而有些只有一个NAN值

时间:2020-09-13 17:14:45

标签: python pandas dataframe data-cleaning

我有这个DF

   Instrument  Historic P/E
0         RMD     44.983729
1         RMD     42.941396
2         RMD     32.880452
3         RMD     32.408159
4         RMD     25.393574
5         RMD     22.792795
6         RMD     21.168423
7         RMD     21.513424
8         RMD     18.280036
9         RMD     21.433815
10    GOOGL.O     27.243951
11    GOOGL.O     23.910224
12    GOOGL.O     32.960744
13    GOOGL.O     28.426405
14    GOOGL.O     34.064604
15    GOOGL.O     26.769495
16    GOOGL.O     31.409884
17    GOOGL.O     20.366475
18    GOOGL.O     21.726667
19    GOOGL.O     22.597021
20     AMZN.O     80.368614
21     AMZN.O     75.734671
22     AMZN.O    256.929249
23     AMZN.O    153.073431
24     AMZN.O    540.937030
25     AMZN.O           NaN
26     AMZN.O    676.775562
27     AMZN.O           NaN
28     AMZN.O    126.464829
29     AMZN.O     71.249881
30     BILI.O           NaN
31     UBER.K           NaN
32        MMM     22.587166
33        MMM     20.761102
34        MMM     25.660342
35        MMM     21.877466
36        MMM     19.860905
37        MMM     21.949134
38        MMM     20.879453
39        MMM     14.694292
40        MMM     13.720270
41        MMM     15.326963
42     MSFT.O     35.310141
43     MSFT.O     26.362191
44     MSFT.O     25.389494
45     MSFT.O     21.180100
46     MSFT.O     19.963249
47     MSFT.O     29.887221
48     MSFT.O     15.866554
49     MSFT.O     13.383155

我希望有一个DF来显示每只股票的最大本益比,如果它是NA,则保持NA

0         RMD     44.983729
1    GOOGL.O     34.064604
2     AMZN.O    676.775562
3     BILI.O           NaN
4     UBER.K           NaN

谢谢

1 个答案:

答案 0 :(得分:1)

您在这里:

max_df = df.groupby('RMD').max()