1
私は、データフレームを持っている:作成サマリー行は
df = pd.DataFrame({'State': {0: "AZ", 1: "AZ", 2:"AZ", 4: "AZ", 5: "AK", 6: "AK", 7 : "AK", 8: "AK"},
'City': {0: "A", 1: "A", 2:"B", 4: "B", 5: "C", 6: "C", 7 : "D", 8: "D"},
'Area': {0: "North", 1: "South", 2:"North", 4: "South", 5: "North", 6: "South", 7 : "North", 8: "South"},
'Restaurant': {0: "Rest1", 1: "Rest2", 2:"Rest3", 4: "Rest4", 5: "Rest5", 6: "Rest6", 7 : "Rest7", 8: "Rest8"},
'Price': {0: 2343, 1: 23445, 2:34536, 4: 7456, 5: 6584, 6: 64563, 7 : 54745, 8: 436345}},
columns=['State','City','Area','Restaurant','Price'])
print(df)
State City Area Restaurant Price
0 AZ A North Rest1 2343
1 AZ A South Rest2 23445
2 AZ B North Rest3 34536
...
私も、次のピボットテーブルを持っている:
私はそれぞれを集約し、「すべて」の行を計算することができるようにしたいpivo=pd.pivot_table(df,values=["Price"],
columns=['State',"City", 'Area'],
margins=True,
aggfunc=[len, np.mean])
print(pivo)
len mean
State City Area
Price AK C North 1 6584.000
South 1 64563.000
D North 1 54745.000
South 1 436345.000
AZ A North 1 2343.000
South 1 23445.000
B North 1 34536.000
South 1 7456.000
All 8 78752.125
州と各都市は次のようになります:
len mean
State City Area
Price AK All 4 281118.5
C All 2 35573.5
North 1 6584.000
South 1 64563.000
D All 2 245545
North 1 54745.000
South 1 436345.000
...
私はスタック/スタックで遊んでいましたが、私は生産していません何か近くに。
ありがとうございました!
編集:
pivo=pd.pivot_table(df,values=["Price"],
index=['State'],
columns=["City", 'Area'],
margins=True,
aggfunc=[len, np.mean])
len mean
Price Price
State City Area
AK All 4.0 140559.000
C North 1.0 6584.000
South 1.0 64563.000
D North 1.0 54745.000
South 1.0 436345.000
AZ A North 1.0 2343.000
South 1.0 23445.000
All 4.0 16945.000
B North 1.0 34536.000
South 1.0 7456.000
All A North 1.0 2343.000
South 1.0 23445.000
All 8.0 78752.125
B North 1.0 34536.000
South 1.0 7456.000
C North 1.0 6584.000
South 1.0 64563.000
D North 1.0 54745.000
South 1.0 436345.000