@@ -700,24 +700,44 @@ def test_margin_normalize(self):
700
700
tm .assert_frame_equal (result , expected )
701
701
702
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'
703
+ df = DataFrame (
704
+ {
705
+ "A" : ["foo" , "foo" , "foo" , "foo" , "foo" , "bar" , "bar" , "bar" , "bar" ],
706
+ "B" : ["one" , "one" , "one" , "two" , "two" , "one" , "one" , "two" , "two" ],
707
+ "C" : [
708
+ "small" ,
709
+ "large" ,
710
+ "large" ,
711
+ "small" ,
712
+ "small" ,
713
+ "large" ,
714
+ "small" ,
715
+ "small" ,
716
+ "large" ,
717
+ ],
718
+ "D" : [1 , 2 , 2 , 3 , 3 , 4 , 5 , 6 , 7 ],
719
+ "E" : [2 , 4 , 5 , 5 , 6 , 6 , 8 , 9 , 9 ],
720
+ }
721
+ )
722
+ result = crosstab (
723
+ index = df .C ,
724
+ columns = [df .A , df .B ],
725
+ margins = True ,
726
+ margins_name = "margin" ,
727
+ normalize = True ,
728
+ )
729
+ expected = DataFrame (
730
+ [
731
+ [0.111111 , 0.111111 , 0.222222 , 0.000000 , 0.444444 ],
732
+ [0.111111 , 0.111111 , 0.111111 , 0.222222 , 0.555556 ],
733
+ [0.222222 , 0.222222 , 0.333333 , 0.222222 , 1.0 ],
734
+ ],
735
+ index = ["large" , "small" , "margin" ],
736
+ )
737
+ expected .columns = MultiIndex (
738
+ levels = [["bar" , "foo" , "margin" ], ["" , "one" , "two" ]],
739
+ codes = [[0 , 0 , 1 , 1 , 2 ], [1 , 2 , 1 , 2 , 0 ]],
740
+ names = ["A" , "B" ],
741
+ )
742
+ expected .index .name = "C"
722
743
tm .assert_frame_equal (result , expected )
723
-
0 commit comments