python
1# 本来はcsvを読み込んでいます。それにあわせるために、astype(int)しました。2import pandas as pd 3 4df = pd.DataFrame([5['D001','1','S02','P001','丸','13','37'],6['D001','1','S03','P001','丸','6','28'],7['D001','1','S05','P001','丸','2','20'],8['D001','2','S02','P002','三角','10','10'],9['D001','2','S05','P002','三角','5','6'],10['D001','3','S02','P003','四角','11','11'],11['D002','1','S02','P004','線','15','12'],12['D002','1','S03','P004','線','9','8'],13['D003','1','S02','P002','三角','10','10'],14['D003','1','S06','P002','三角','2','3'],15['D003','2','S02','P002','三角','10','10'],16['D003','2','S05','P002','三角','5','6'],17['D003','3','S01','P005','丸','1','1']18],19columns=['ID1','ID2','種類1','種類2','種類2名','値1','値2'],20)21 22df['ID2'] = df['ID2'].astype(int)23df['値1'] = df['値1'].astype(int)24df['値2'] = df['値2'].astype(int)25 26dfg = df.groupby(['ID1','ID2']).sum()27print(dfg)
0 コメント