根据另一列的结果减去DataFrame中的列

时间:2016-12-18 20:05:32

标签: python pandas

假设我有一个Pandas DataFrame,如下表所示:

CarFuel Volume Mazda 311.3 Mazda 310.4 F-15014.3 F-1509.7



<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 122px">
<colgroup>
<col style="width: 56px">
<col style="width: 66px">
</colgroup>
  <tr>
    <th class="tg-yw4l">Car</th>
    <th class="tg-yw4l">Fuel Volume</th>
  </tr>
  <tr>
    <td class="tg-yw4l">F-150</td>
    <td class="tg-yw4l">25.01</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-150</td>
    <td class="tg-yw4l">22.47</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-150</td>
    <td class="tg-yw4l">19.56</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-250</td>
    <td class="tg-yw4l">9.87</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-250</td>
    <td class="tg-yw4l">6.32</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-250</td>
    <td class="tg-yw4l">1.32</td>
  </tr>
</table>
&#13;
&#13;
&#13;

我想根据燃油量的差异创建另一列,但前提是减去的燃油量来自同一型号的汽车。因此,生成的DataFrame将如下所示:

&#13;
&#13;
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 0px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 0px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 144px">
<colgroup>
<col style="width: 52px">
<col style="width: 62px">
<col style="width: 30px">
</colgroup>
  <tr>
    <th class="tg-yw4l">Car</th>
    <th class="tg-yw4l">Fuel Volume</th>
    <th class="tg-yw4l">Difference in Fuel</th>
  </tr>
  <tr>
    <td class="tg-yw4l">F-150</td>
    <td class="tg-yw4l">25.01</td>
    <td class="tg-yw4l">NaN</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-150</td>
    <td class="tg-yw4l">22.47</td>
    <td class="tg-yw4l">2.54</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-150</td>
    <td class="tg-yw4l">19.56</td>
    <td class="tg-yw4l">2.91</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-250</td>
    <td class="tg-yw4l">9.87</td>
    <td class="tg-yw4l">NaN</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-250</td>
    <td class="tg-yw4l">6.32</td>
    <td class="tg-yw4l">3.55</td>
  </tr>
  <tr>
    <td class="tg-yw4l">F-250</td>
    <td class="tg-yw4l">1.32</td>
    <td class="tg-yw4l">5</td>
  </tr>
</table>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我认为您需要groupby diff,最后添加abs

df['Difference in Fuel'] = df.groupby('Car')['Fuel Volume'].diff().abs()
print (df)
     Car  Fuel Volume  Difference in Fuel
0  F-150        25.01                 NaN
1  F-150        22.47                2.54
2  F-150        19.56                2.91
3  F-250         9.87                 NaN
4  F-250         6.32                3.55
5  F-250         1.32                5.00