pandas: append new column of row subtotals
<p>This is very similar to <a href="https://stackoverflow.com/questions/14059094/i-want-to-multiply-two-columns-in-a-pandas-dataframe-and-add-the-result-into-a-n?rq=1">this question</a>, except I want my code to be able to apply to the length of a dataframe, instead of specific columns.</p>

<p>I have a DataFrame, and I'm trying to get a sum of each row to append to the dataframe as a column.</p>

<pre><code>df = pd.DataFrame([[1,0,0],[20,7,1],[63,13,5]],columns=['drinking','drugs','both'],index = ['First','Second','Third'])

        drinking  drugs  both
First          1      0     0
Second        20      7     1
Third         63     13     5
</code></pre>

<p>Desired output:</p>

<pre><code>        drinking  drugs  both  total
First          1      0     0      1
Second        20      7     1     28
Third         63     13     5     81
</code></pre>

<p>Current code: </p>

<pre><code>df['total'] = df.apply(lambda row: (row['drinking'] + row['drugs'] + row['both']),axis=1)
</code></pre>

<p>This works great. But what if I have another dataframe, with seven columns, which are not called 'drinking', 'drugs', or 'both'? Is it possible to adjust this function so that it applies to the length of the dataframe? That way I can use the function for any dataframe at all, with a varying number of columns, not just a dataframe with columns called 'drinking', 'drugs', and 'both'? </p>

<p>Something like:</p>

<pre><code>df['total'] = df.apply(for col in df: [code to calculate sum of each row]),axis=1)
</code></pre>