pandas: sum two rows of dataframe without rearranging dataframe?
<p>I have a dataframe and I'm trying to sum two rows without messing up the order of the rows.</p>

<pre><code>&gt; test = {'counts' : pd.Series([10541,4143,736,18,45690], index=['Daylight','Dawn','Other / unknown','Uncoded &amp; errors','Total']), 'percents' : pd.Series([23.07,9.07,1.61,0.04,100], index=['Daylight','Dawn','Other / unknown','Uncoded &amp; errors','Total'])}

&gt; testdf = pd.DataFrame(test)

                  counts  percents
Daylight           10541     23.07
Dawn                4143      9.07
Other / unknown      736      1.61
Uncoded &amp; errors      18      0.04
Total              45690    100.00
</code></pre>

<p>I want this output:</p>

<pre><code>                  counts  percents
Daylight           10541     23.07
Dawn                4143      9.07
Other / unknown      754      1.65   &lt;-- sum of 'other/unknown' and 'uncoded &amp; errors'
Total              45690    100.00
</code></pre>

<p>This is as close as I've been able to get:</p>

<pre><code>&gt; sum_ = testdf.loc[['Other / unknown', 'Uncoded &amp; errors']].sum().to_frame().transpose()

     counts   percents
0    754.00   1.65       

&gt; sum_ = sum_.rename(index={0: 'Other / unknown'})

                counts   percents
Other / unknown 754.00   1.65   

&gt; testdf.drop(['Other / unknown', 'Uncoded &amp; errors'],inplace=True)
&gt; testdf = testdf.append(sum_)

Daylight         10541  23.07
Dawn             4143   9.07
Total            45690  100
Other / unknown  754    1.65
</code></pre>

<p>But this does not preserve the order of the original rows </p>

<p>I could insert the row by slicing the dataframe and inserting the sum_ row between 'Dawn' and 'Total', but that will not work if the row labels ever change, or if the order of the rows change, etc. (this is an annual brochure so the table design might change from year to year), so I'm trying to do this robustly.</p>