Pandas Dataframe - 用倒数替换列值

时间:2017-11-20 09:07:06

标签: python pandas

我的数据框如下所示

           0     1         2         3         4
           0   0.660377  0.75  0.728395  1.000000  0.011364
           1   0.452830  0.50  0.629630  0.083333  0.045455
           2   0.971698  0.75  0.975309  0.166667  0.079545
           3   0.169811  0.25  0.172840  0.291667  0.068182
           4   0.216981  0.25  0.222222  0.000000  0.090909
           5   0.669811  0.50  0.839506  0.333333  0.045455

我需要用倒数值替换第一列。在熊猫中有没有内置的方法呢?

1 个答案:

答案 0 :(得分:5)

我认为你需要与<!-- Path directly embedded in SVG --> <div class="wrapper"> <div>Clicking the star toggles the color in all browsers.</div> <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="120" height="120" version="1.1"> <circle cx="60" cy="60" r="60" fill="lightgrey"></circle> <path stroke="#000000" stroke-width="1" fill="lightblue" d=" M 60.000 80.000 L 80.000 94.641 L 77.321 70.000 L 100.000 60.000 L 77.321 50.000 L 80.000 25.359 L 60.000 40.000 L 40.000 25.359 L 42.679 50.000 L 20.000 60.000 L 42.679 70.000 L 40.000 94.641 z"/> </svg> </div> <!-- Path embedded in SVG with the use tag --> <div class="wrapper"> <div>Clicking the star toggles the color in Chrome but not in FF.</div> <svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="120" height="120" version="1.1"> <defs> <path id="star" stroke="#000000" stroke-width="1" fill="lightblue" d=" M 60.000 80.000 L 80.000 94.641 L 77.321 70.000 L 100.000 60.000 L 77.321 50.000 L 80.000 25.359 L 60.000 40.000 L 40.000 25.359 L 42.679 50.000 L 20.000 60.000 L 42.679 70.000 L 40.000 94.641 z"/> </defs> <circle cx="60" cy="60" r="60" fill="lightgrey"></circle> <use xlink:href="#star"></use> </svg> </div>分开:

1

或使用DataFrame.rdiv

df[0] = 1 / df[0]
print (df)
          0     1         2         3         4
0  1.514287  0.75  0.728395  1.000000  0.011364
1  2.208334  0.50  0.629630  0.083333  0.045455
2  1.029126  0.75  0.975309  0.166667  0.079545
3  5.888900  0.25  0.172840  0.291667  0.068182
4  4.608698  0.25  0.222222  0.000000  0.090909
5  1.492958  0.50  0.839506  0.333333  0.045455

或使用numpy.reciprocal

df[0] = df[0].rdiv(1)
print (df)
          0     1         2         3         4
0  1.514287  0.75  0.728395  1.000000  0.011364
1  2.208334  0.50  0.629630  0.083333  0.045455
2  1.029126  0.75  0.975309  0.166667  0.079545
3  5.888900  0.25  0.172840  0.291667  0.068182
4  4.608698  0.25  0.222222  0.000000  0.090909
5  1.492958  0.50  0.839506  0.333333  0.045455