Aggregating data with groupby

Group the data using groupby and perform the operation.

Get the average of the pre-sale price by region name and visualize it as a bar graph. .groupby(["Column name to use as index"])["Column value to calculate"].Operation()

# df.groupby(["인덱스로 사용할 컬럼명"])["계산할 컬럼 값"].연산()

df_last.groupby(["지역명"])["평당분양가격"].mean()
지역명
강원     7890.750000
경기    13356.895200
경남     9268.778138
경북     8376.536515
광주     9951.535821
대구    11980.895455
대전    10253.333333
부산    12087.121200
서울    23599.976400
세종     9796.516456
울산    10014.902013
인천    11915.320732
전남     7565.316532
전북     7724.235484
제주    11241.276712
충남     8233.651883
충북     7634.655600
Name: 평당분양가격, dtype: float64

Calculate the average of the pre-sale price by exclusive area.

df_last.groupby(["Exclusive Area"])["평당분양가격"].mean()
Exclusive Area
102㎡~       11517.705634
60㎡         10375.137421
60㎡~85㎡     10271.040071
85㎡~102㎡    11097.599573
전체          10276.086207
Name: 평당분양가격, dtype: float64
# Multiple Indexes : 지역명, 전용면적으로 평당분양가격의 평균을 구합니다.
df_last.groupby(["지역명", "Exclusive Area"])["평당분양가격"].mean()
지역명  Exclusive Area
강원   102㎡~             8311.380000
     60㎡               7567.098000
     60㎡~85㎡           7485.588000
     85㎡~102㎡          8749.557143
     전체                7477.536000
                          ...     
충북   102㎡~             8195.352000
     60㎡               7103.118000
     60㎡~85㎡           7264.488000
     85㎡~102㎡          8391.306000
     전체                7219.014000
Name: 평당분양가격, Length: 85, dtype: float64
  • Unstack : Adding the result into the column
  • round() : Removing decimal places
df_last.groupby(["Exclusive Area", "지역명"])["평당분양가격"].mean().unstack().round() 
지역명 강원 경기 경남 경북 광주 대구 대전 부산 서울 세종 울산 인천 전남 전북 제주 충남 충북
Exclusive Area
102㎡~ 8311.0 14772.0 10358.0 9157.0 11042.0 13087.0 14877.0 13208.0 23446.0 10107.0 9974.0 14362.0 8168.0 8194.0 10523.0 8689.0 8195.0
60㎡ 7567.0 13252.0 8689.0 7883.0 9431.0 11992.0 9176.0 11354.0 23213.0 9324.0 9202.0 11241.0 7210.0 7610.0 14022.0 7911.0 7103.0
60㎡~85㎡ 7486.0 12524.0 8619.0 8061.0 9911.0 11779.0 9711.0 11865.0 22787.0 9775.0 10503.0 11384.0 7269.0 7271.0 10621.0 7819.0 7264.0
85㎡~102㎡ 8750.0 13678.0 10018.0 8774.0 9296.0 11141.0 9037.0 12073.0 25944.0 9848.0 8861.0 11528.0 7909.0 8276.0 10709.0 9120.0 8391.0
전체 7478.0 12560.0 8659.0 8079.0 9904.0 11771.0 9786.0 11936.0 22610.0 9805.0 10493.0 11257.0 7284.0 7293.0 10785.0 7815.0 7219.0

Calculate the average sale price per pyeong by year and region name.

  • T: Transposing the column & row
g = df_last.groupby(["연도", "지역명"])["평당분양가격"].mean().unstack().T
g
연도 2015 2016 2017 2018 2019
지역명
강원 7188.060 7162.903846 7273.560000 8219.255000 8934.475000
경기 11060.940 11684.970000 12304.980000 14258.420000 15665.540000
경남 8459.220 8496.730000 8786.760000 9327.670000 10697.615789
경북 7464.160 7753.405000 8280.800000 8680.776923 9050.250000
광주 7916.700 9190.683333 9613.977551 9526.953333 12111.675000
대구 9018.900 10282.030000 12206.700000 12139.252632 14081.650000
대전 8190.600 8910.733333 9957.158491 10234.106667 12619.200000
부산 10377.400 10743.535000 11560.680000 12889.965000 13537.865000
서울 20315.680 21753.435000 21831.060000 23202.245000 28286.830000
세종 8765.020 8857.805000 9132.505556 10340.463158 11299.394118
울산 9367.600 9582.574138 10666.935714 10241.400000 10216.250000
인천 10976.020 11099.055000 11640.600000 11881.532143 13249.775000
전남 6798.880 6936.600000 7372.920000 7929.845000 8219.275862
전북 7110.400 6906.625000 7398.973585 8174.595000 8532.260000
제주 7951.075 9567.480000 12566.730000 11935.968000 11828.469231
충남 7689.880 7958.225000 8198.422222 8201.820000 8748.840000
충북 6828.800 7133.335000 7473.120000 8149.295000 7970.875000

Aggregate data with pivot table

  • Do the same with pivot_table as you did with groupby.

Get the area name as index and the sale price per pyeong as values.

pd.pivot_table(df_last, index=["지역명"], values=["평당분양가격"], aggfunc="mean")
평당분양가격
지역명
강원 7890.750000
경기 13356.895200
경남 9268.778138
경북 8376.536515
광주 9951.535821
대구 11980.895455
대전 10253.333333
부산 12087.121200
서울 23599.976400
세종 9796.516456
울산 10014.902013
인천 11915.320732
전남 7565.316532
전북 7724.235484
제주 11241.276712
충남 8233.651883
충북 7634.655600
# The biggest difference between groupby and pivot_table : the result shown as in series or pivot table datasets.
# The loading time can be differenciated

df_last.groupby(["Exclusive Area"])["평당분양가격"].mean()
Exclusive Area
102㎡~       11517.705634
60㎡         10375.137421
60㎡~85㎡     10271.040071
85㎡~102㎡    11097.599573
전체          10276.086207
Name: 평당분양가격, dtype: float64
# pivot : aggregation function x / cannot perform operation / only shows datasets
# pivot_table : function o 
pd.pivot_table(df_last, index="Exclusive Area", values="평당분양가격")
평당분양가격
Exclusive Area
102㎡~ 11517.705634
60㎡ 10375.137421
60㎡~85㎡ 10271.040071
85㎡~102㎡ 11097.599573
전체 10276.086207
# 연도, 지역명으로 평당분양가격의 평균을 구합니다.
# g = df_last.groupby(["연도", "지역명"])["평당분양가격"].mean()
# groupby is much faster
p = pd.pivot_table(df_last, index="연도", values="평당분양가격")
p.loc[2017]
평당분양가격    10360.487653
Name: 2017, dtype: float64

Leave a comment