将某些行中的值添加到另一行

时间:2018-03-12 18:55:21

标签: python pandas dataframe

我有一个像这样的pandas DataFrame:

<div class="sheet-paper">
 <div class="sheet-report">
  <div class="page-header">
    <!-- All header things -->
  </div>

  <div class="page-report">
   <table class="table table-striped" table style="table-layout: fixed;">
    <thead class="thead-light">
        <tr class="active">
            <th class="text-center text-primary" style="font-size: 11px; width:15%">Item nº</th>
            <th class="text-center text-primary" style="font-size: 11px; width:37%">Descrição</th>
            <th class="text-center text-primary" style="font-size: 11px; width:16%">Preço Unit. </th>
            <th class="text-center text-primary" style="font-size: 11px; width:16%">Quantidade </th>
            <th class="text-center text-primary" style="font-size: 11px; width:16%">Preço </th>
        </tr>
    </thead>

    <tbody>
        <?php for($i=0; $i< (count($results) - 1); $i++){?>
            <tr class="page-break">
                <td class="text-left" style="font-size: 10px;"><?php echo $results[$i]['item_n']; ?></td>
                <td class="text-left" style="font-size: 10px;"><?php echo $results[$i]['description']; ?></td>
                <td class="text-right" style="font-size: 10px;"><?php echo number_format($results[$i]['unit_price'], 2); ?>€</td>
                <td class="text-right" style="font-size: 10px;"><?php echo number_format($results[$i]['qty'], 0); ?></td>
                <td class="text-right" style="font-size: 10px;"><?php echo number_format($results[$i]['price'], 2); ?>€<span class="invisible">)</span></td>
            </tr>
        <?php } ?>
     </tbody>
    </table>
   </div>
  </div>
  <div class="page-footer">
   <!-- All footer things -->
  </div>
</div>



@media print {
 .page-header {
 position: fixed;
 width: 19.5cm;
 top: -25px;
 }
.page-report {
 position: fixed;
 top: 8.3cm;
 width: 19.5cm;
 height: 12.7cm;
}
.page-footer {
 position: fixed;
 width: 19.5cm;
 bottom: 0;
 page-break-before: auto;
}
.btn-report {
 display: none;
}
table tr.page-break {
 page-break-after: auto;
 max-height: 8cm
}
table {
 page-break-inside: auto
}
}

如果适用,我希望将import pandas as pd df = pd.DataFrame({ 'cohort': [1, 1, 1, 1, 2, 2, 2], 'age': [-1, 0, 1, 2, 0, 1, 2], 'bal': [100, 1000, 1400, 1500, 1000, 1200, 1300] }) bal小于0的age添加到每个bal age为零的cohort值。最终我希望df看起来像这样:

df
  cohort age  bal
1      1  -1  100
2      1   0 1100
3      1   1 1400
4      1   2 1500
5      2   0 1000
6      2   1 1200
7      2   2 1300

1 个答案:

答案 0 :(得分:0)

这就是A | B | C | Q1 | Q2 1 1/1 1/12 20 20 1 1/7 1/17 30 30 1 1/21 1/17 15 10 2 1/1 1/14 10 10 2 1/21 1/25 30 30 2 1/30 NULL 5 0 实现目标的方式

pandas

更新:

df.loc[df.age==0, 'bal']= df.loc[df.age==0, 'bal']+ df.loc[df.age<0, 'bal'].values
df
Out[339]: 
   age   bal  cohort
0   -1   100       1
1    0  1100       1
2    1  1400       1
3    2  1500       1
4   -1   120       2
5    0  1120       2
6    1  1200       2
7    2  1300       2