@@ -698,3 +698,26 @@ def test_margin_normalize(self):
698
698
names = ["A" , "B" ],
699
699
)
700
700
tm .assert_frame_equal (result , expected )
701
+
702
+ def test_crosstab_multiple_columns_normalize (self ):
703
+ df = DataFrame ({"A" : ["foo" , "foo" , "foo" , "foo" , "foo" ,
704
+ "bar" , "bar" , "bar" , "bar" ],
705
+ "B" : ["one" , "one" , "one" , "two" , "two" ,
706
+ "one" , "one" , "two" , "two" ],
707
+ "C" : ["small" , "large" , "large" , "small" ,
708
+ "small" , "large" , "small" , "small" ,
709
+ "large" ],
710
+ "D" : [1 , 2 , 2 , 3 , 3 , 4 , 5 , 6 , 7 ],
711
+ "E" : [2 , 4 , 5 , 5 , 6 , 6 , 8 , 9 , 9 ]})
712
+ result = crosstab (index = df .C ,
713
+ columns = [df .A , df .B ],
714
+ margins = True ,
715
+ margins_name = 'margin' , normalize = True )
716
+ expected = DataFrame ([[0.111111 , 0.111111 , 0.222222 , 0.000000 , 0.444444 ],
717
+ [0.111111 , 0.111111 , 0.111111 , 0.222222 , 0.555556 ],
718
+ [0.222222 , 0.222222 , 0.333333 , 0.222222 , 1. ]],
719
+ index = ['large' , 'small' , 'margin' ])
720
+ expected .columns = MultiIndex ( levels = [['bar' , 'foo' , 'margin' ], ['' , 'one' , 'two' ]], codes = [[0 , 0 , 1 , 1 , 2 ], [1 , 2 , 1 , 2 , 0 ]], names = ["A" , "B" ], )
721
+ expected .index .name = 'C'
722
+ tm .assert_frame_equal (result , expected )
723
+
0 commit comments