I have the following code:
import numpy as np
import pandas as pd
obs = pd.DataFrame({
'storm': [1, 1, 1, 1, 0, 0, 0, 0],
'lightning': [1, 1, 0, 0, 1, 1, 0, 0],
'thunder': [1, 0, 1, 0, 1, 0, 1, 0],
'p': [0.20, 0.05, 0.04, 0.36, 0.04, 0.01, 0.03, 0.27]
})
g1=obs.groupby(['lightning','thunder']).agg({'p':'sum'})
g2=obs.groupby(['lightning','thunder','storm']).agg({'p':'sum'})
which gives
Now how to divide more detailed groupby by less detailed (to calculate percentage)?
I have read this Pandas percentage of total with groupby but was unable to derive how to rewrite for my case.