假设我有一个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;
我想根据燃油量的差异创建另一列,但前提是减去的燃油量来自同一型号的汽车。因此,生成的DataFrame将如下所示:
<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;