从这样的数据框开始:
df = pd.DataFrame({"Cat1":['a','a', 'b','b','c','c','c', 'c', 'c'], "Cat2":[1,1,1,2,1,1,2,2,2]})
df
Out[2]:
Cat1 Cat2
0 a 1
1 a 1
2 b 1
3 b 2
4 c 1
5 c 1
6 c 2
7 c 2
8 c 2
我如何操纵数据以产生每个" Cat1" - " Cat2"对,按升序排列?结果如下:
df = pd.DataFrame({"Cat1":['a','a', 'b','b','c','c','c', 'c', 'c'], "Cat2":[1,1,1,2,1,1,2,2,2], "Count":[1,2,1,1,1,2,1,2,3]})
df
Out[4]:
Cat1 Cat2 Count
0 a 1 1
1 a 1 2
2 b 1 1
3 b 2 1
4 c 1 1
5 c 1 2
6 c 2 1
7 c 2 2
8 c 2 3