Sum up values in a column using Pandas
<p>I have a dataframe where one column has a list of zipcodes and the other has property values corresponding to the zipcode. I want to sum up the property values in each row according to the appropriate zipcode. </p>

<p>So, for example:</p>

<pre><code>zip  value
2210 $5,000
2130 $3,000
2210 $2,100
2345 $1,000
</code></pre>

<p>I would then add up the values </p>

<pre><code>$5,000 + $2,100  = $7,100 
</code></pre>

<p>and reap the total property value of for zipcode 2210 as $7,100. </p>

<p>Any help in this regard will be appreciated</p>

<p>You need:</p>

<pre><code>df
    zip  value
0  2210   5000
1  2130   3000
2  2210   2100
3  2345   1000

df2 = df.groupby(['zip'])['value'].sum()

df2 
zip    value
2130    3000
2210    7100
2345    1000
Name: value, dtype: int64
</code></pre>

<p>You can read more about it <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html" rel="nofollow">here</a>.</p>

<p>Also you will need to remove the $ sign in the column values. For that you can use something along the lines of the following while reading the dataframe initially:</p>

<pre><code>df = pd.read_csv('zip_value.csv', header=0,names=headers,converters={'value': lambda x: float(x.replace('$',''))})
</code></pre>

<p>Edit: Changed the code according to comment.
To reset the index after groupby use:</p>

<pre><code>df2 = df.groupby(['zip'])['value'].sum().reset_index()
</code></pre>

<p>Then to remove a particular column with zip value ,say, 2135 , you need</p>

<pre><code>df3 = df2[df2['zip']!= 2135]
</code></pre>

python pandas dataframe sum