熊猫:如何组合行

时间:2018-01-02 18:25:43

标签: python pandas dataframe data-science



'<table border="1" class="dataframe">  <thead>    <tr style="text-align: right;">      <th></th>      <th>CDS_CODE</th>      <th>COUNTY</th>      <th>DISTRICT</th>      <th>SCHOOL</th>      <th>KDGN</th>      <th>GR_1</th>      <th>GR_2</th>      <th>GR_3</th>      <th>GR_4</th>      <th>GR_5</th>      <th>GR_6</th>      <th>GR_7</th>      <th>GR_8</th>      <th>UNGR_ELM</th>      <th>GR_9</th>      <th>GR_10</th>      <th>GR_11</th>      <th>GR_12</th>      <th>UNGR_SEC</th>      <th>ENR_TOTAL</th>      <th>ADULT</th>    </tr>  </thead>  <tbody>    <tr>      <th>0</th>      <td>1611436056865</td>      <td>Alameda</td>      <td>Berkeley Unified</td>      <td>Willard Middle</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>12</td>      <td>16</td>      <td>6</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>34</td>      <td>0</td>    </tr>    <tr>      <th>1</th>      <td>1611436056865</td>      <td>Alameda</td>      <td>Berkeley Unified</td>      <td>Willard Middle</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>1</td>      <td>0</td>      <td>3</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>4</td>      <td>0</td>    </tr>    <tr>      <th>2</th>      <td>1611436056865</td>      <td>Alameda</td>      <td>Berkeley Unified</td>      <td>Willard Middle</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>23</td>      <td>30</td>      <td>22</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>75</td>      <td>0</td>    </tr>    <tr>      <th>3</th>      <td>1611436056865</td>      <td>Alameda</td>      <td>Berkeley Unified</td>      <td>Willard Middle</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>1</td>      <td>0</td>      <td>2</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>3</td>      <td>0</td>    </tr>    <tr>      <th>4</th>      <td>1611436056865</td>      <td>Alameda</td>      <td>Berkeley Unified</td>      <td>Willard Middle</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>5</td>      <td>15</td>      <td>13</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>0</td>      <td>33</td>      <td>0</td>    </tr>  </tbody></table>'
&#13;
&#13;
&#13;

这是我数据框的负责人。我想知道是否有办法将每个年级水平结合在一起。也就是说,将这5行合并为1行。帮助??

1 个答案:

答案 0 :(得分:1)

让我们试试这个:

df.filter(like='GR').sum()

输出:

GR_1         0
GR_2         0
GR_3         0
GR_4         0
GR_5         0
GR_6        42
GR_7        61
GR_8        46
UNGR_ELM     0
GR_9         0
GR_10        0
GR_11        0
GR_12        0
UNGR_SEC     0