I am trying to get similar excel functionality in pandas, mainly a % of type behavior. Using the following data:
{'A': ['a', 'b', 'b', 'a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'a', 'b'],
'C': ['e', 'e', 'e', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'e', 'e'],
'B': ['c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd', 'c', 'c', 'd'],
'D': ['g', 'g', 'h', 'h', 'g', 'g', 'h', 'h', 'g', 'g', 'h', 'h', 'g'],
'V1': [84.0, 440.0, 423.0, 63.0, 990.0, 192.0, 169.0, 387.0, 934.0, 208.0, 834.0, 923.0, 230.0],
'V2': [120.0, 942.0, 153.0, 284.0, 517.0, 695.0, 37.0, 30.0, 237.0, 56.0, 15.0, 696.0, 25.0]}
I create a DataFrame
object called df1 from this dictionary.
I want to display ultimately:
B C V1 V2 V2 as Percent of B
c e 1870 1911 0.700770077
c f 1887 816 0.299229923
d e 230 25 0.023148148
d f 1890 1055 0.976851852
I can get from Pandas pivot_table by executing pivot_table(df1,values=['V1','V2'],rows=['B','C'],aggfunc=numpy.sum,fill_value=0)
:
V1 V2
B C
c e 1870 1911
f 1887 816
d e 230 25
f 1890 1055
anyone have an idea how to do that last step to get the column?
Thanks! Jon