Add values to bottom of DataFrame automatically with Pandas
<p>I'm initializing a DataFrame:</p>

<pre><code>columns = ['Thing','Time']
df_new = pd.DataFrame(columns=columns)
</code></pre>

<p>and then writing values to it like this:</p>

<pre><code>for t in df.Thing.unique():
    df_temp = df[df['Thing'] == t]  #filtering the df
    df_new.loc[counter,'Thing'] = t #writing the filter value to df_new
    df_new.loc[counter,'Time'] = dftemp['delta'].sum(axis=0) #summing and adding that value to the df_new
    counter += 1 #increment the row index
</code></pre>

<p>Is there are better way to add new values to the dataframe each time without explicitly incrementing the row index with 'counter'?</p>

<p>If I'm interpreting this correctly, I think this can be done in one line:</p>

<pre><code>newDf = df.groupby('Thing')['delta'].sum().reset_index()
</code></pre>

<p>By grouping by 'Thing', you have the various "t-filters" from your for-loop. We then apply a <code>sum()</code> to 'delta', but only within the various "t-filtered" groups. At this point, the dataframe has the various values of "t" as the indices, and the sums of the "t-filtered deltas" as a corresponding column. To get to your desired output, we then bump the "t's" into their own column via <code>reset_index()</code>.</p>

python pandas