I'm working with 20 years of data. The important columns right now are YEAR, MONTH, NUM1, and NUM2. How can I get the monthly percent of NUM1/NUM2?
YEAR | MONTH | NUM1 | NUM2 |
------------------------------
2000 | 6 | 60 | 100 |
2000 | 6 | 55 | 100 |
2000 | 2 | 80 | 160 |
to
YEAR | MONTH | NUM1 | NUM2 | PCT |
-------------------------------------
2000 | 2 | 80 | 160 | 50 |
2000 | 6 | 60 | 100 | 57.5 |
2000 | 6 | 55 | 100 | 57.5 |
What I want to do is
df2 = df.groupby(['YEAR', 'MONTH'], as_index=False)
df2['PCT'] = df2['NUM1']/df2['NUM2']
However, I get TypeError: unsupported operand type(s) for /: 'DataFrameGroupBy' and 'DataFrameGroupBy'
When I run type(df2)
it returns pandas.core.groupby.generic.DataFrameGroupBy
. What am I doing wrong? How can I fix this? It should be pretty simple to order data in chronological order and then divide to columns.