I have a DataFrame with an index called city_id
of cities in the format [city],[state]
(e.g., new york,ny
containing integer counts in the columns. The problem is that I have multiple rows for the same city, and I want to collapse the rows sharing a city_id
by adding their column values. I looked at groupby()
but it wasn't immediately obvious how to apply it to this problem.
Edit:
An example: I'd like to change this:
city_id val1 val2 val3
houston,tx 1 2 0
houston,tx 0 0 1
houston,tx 2 1 1
into this:
city_id val1 val2 val3
houston,tx 3 3 2
if there are ~10-20k rows.