Kulbear Kulbear - 1 month ago 5
Python Question

How to sum values by value in other columns in pandas in Python?

Hi I am dealing with some data by using pandas.

I am facing a problem but here I'll try to simplify it.

Suppose I have a dataset looks like this:

# Incidents Place Month
0 3 A 1
1 5 B 1
2 2 C 2
3 2 B 2
4 6 C 3
5 3 A 1


So I want to sum the # of incidents by the place, that is, I want to have a result like

P #
A 3
B 7(5+2)
C 8(2+6)


stored in a pandas DataFrame. I don't care about other columns at this point.

Next question is, now if I want to use the data in Month column as well, I'd like to have result looks like

P M #
A 1 6(3+3)
B 1 5
B 2 2
C 2 2
C 3 6


How can I achieve these results in pandas? I have tried groupby and some other functions but I cannot reach the point...

Any help is appreciated!

Answer

you can do it this way:

In [35]: df
Out[35]:
   # Incidents Place  Month
0            3     A      1
1            5     B      1
2            2     C      2
3            2     B      2
4            6     C      3
5            3     A      1

In [36]: df.groupby('Place')['# Incidents'].sum().reset_index()
Out[36]:
  Place  # Incidents
0     A            6
1     B            7
2     C            8

In [37]: df.groupby(['Place', 'Month'])['# Incidents'].sum().reset_index()
Out[37]:
  Place  Month  # Incidents
0     A      1            6
1     B      1            5
2     B      2            2
3     C      2            2
4     C      3            6

Please find here a Pandas documentation with lots of examples

Comments