Data Science Workflow (Pandas & Plotly)
Table of Contents
ข้อมูลจาก รายได้แยกตามแหล่งที่มา แยกตามอาชีพ ปี 2554-2559
1) Exploratory Data Analysis (EDA)
เริ่มต้นด้วยการอ่านข้อมูลเข้ามาก่อน
import pandas as pd
df = pd.read_csv("http://thaiopendata.org/sites/default/files/vwFactExpenditureAvgMonthlyIncomeBySourceOfIncome.csv")
df.head()
ProvinceKey | ProvinceName | ProvinceNameEng | RegionKey | Region | RegionEng | SourceOfIncomeKey | SourceOfIncome | SourceOfIncomeEng | Year | FarmCulturePlantAnimalMainlyOwningLand | FarmCulturePlantAnimalMainlyRentingLand | FarmCultureFishingForestryHuntingAgri | EntrepreneursNonAgriBusiness | EmployeeTechnicianManager | EmployeeLabourInAgriForestryFishery | EmployeeLabourInLogisticTransportation | EmployeeClericalSalesServiceWorker | EmployeeWorkerProductionConstruction | EconomicallyInactive | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 10.0 | ค่าจ้างและเงินเดือน | Wages and salaries | 2556.0 | 0.0 | 0.0 | 4828.0 | 2599.0 | 49270.0 | 9500.0 | 14987.0 | 21842.0 | 21839.0 | 788.0 |
1 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 11.0 | กำไรสุทธิจากการทำธุรกิจ | Net profits from business | 2556.0 | 0.0 | 0.0 | 15074.0 | 22155.0 | 4761.0 | 0.0 | 0.0 | 827.0 | 499.0 | 235.0 |
2 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 12.0 | กำไรสุทธิจากการทำการเกษตร | Net profits from farming | 2556.0 | 8046.0 | 0.0 | 25736.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
3 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 13.0 | เงินที่ได้รับเป็นการช่วยเหลือ | From current transfers | 2556.0 | 450.0 | 0.0 | 403.0 | 334.0 | 511.0 | 200.0 | 0.0 | 374.0 | 346.0 | 14925.0 |
4 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 14.0 | รายได้จากทรัพย์สิน | From property income | 2556.0 | 11.0 | 0.0 | 93.0 | 116.0 | 726.0 | 0.0 | 0.0 | 128.0 | 2.0 | 1009.0 |
df.tail()
ProvinceKey | ProvinceName | ProvinceNameEng | RegionKey | Region | RegionEng | SourceOfIncomeKey | SourceOfIncome | SourceOfIncomeEng | Year | FarmCulturePlantAnimalMainlyOwningLand | FarmCulturePlantAnimalMainlyRentingLand | FarmCultureFishingForestryHuntingAgri | EntrepreneursNonAgriBusiness | EmployeeTechnicianManager | EmployeeLabourInAgriForestryFishery | EmployeeLabourInLogisticTransportation | EmployeeClericalSalesServiceWorker | EmployeeWorkerProductionConstruction | EconomicallyInactive | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
773 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 13.0 | เงินที่ได้รับเป็นการช่วยเหลือ | From current transfers | 2556.0 | 501.0 | 0.0 | 300.0 | 607.0 | 431.0 | 257.0 | 150.0 | 254.0 | 287.0 | 6877.0 |
774 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 14.0 | รายได้จากทรัพย์สิน | From property income | 2556.0 | 0.0 | 0.0 | 0.0 | 12.0 | 47.0 | 0.0 | 0.0 | 9.0 | 0.0 | 34.0 |
775 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 15.0 | รายได้ที่ไม่เป็นตัวเงิน | Non-money income | 2556.0 | 2191.0 | 0.0 | 2042.0 | 3127.0 | 2571.0 | 2204.0 | 1855.0 | 2167.0 | 1766.0 | 2587.0 |
776 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 16.0 | รายได้ไม่ประจำ (ที่เป็นตัวเงิน) | Non - current money income | 2556.0 | 0.0 | 0.0 | 0.0 | 3.0 | 0.0 | 0.0 | 70.0 | 0.0 | 0.0 | 0.0 |
777 | \n | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
ปกติแล้วเราจะใช้เวลาในการดูข้อมูลเพื่อทำความคุ้นเคยกับตัวข้อมูล ในกรณีนี้ ข้อมูลเป็นข้อมูลรายได้แยกตามแหล่งที่มาและอาชีพ
เมื่อเราดูข้อมูลจนเกิดไอเดียบางสิ่งบางอย่างที่เราต้องการทราบจากข้อมูล จึงค่อยเริ่มจัดการกับข้อมูลนั้น
ในตัวอย่างนี้จะเริ่มด้วยการทิ้งข้อมูลส่วนที่เราไม่ต้องการ
df = df.drop(df.tail(1).index)
df.tail()
ProvinceKey | ProvinceName | ProvinceNameEng | RegionKey | Region | RegionEng | SourceOfIncomeKey | SourceOfIncome | SourceOfIncomeEng | Year | FarmCulturePlantAnimalMainlyOwningLand | FarmCulturePlantAnimalMainlyRentingLand | FarmCultureFishingForestryHuntingAgri | EntrepreneursNonAgriBusiness | EmployeeTechnicianManager | EmployeeLabourInAgriForestryFishery | EmployeeLabourInLogisticTransportation | EmployeeClericalSalesServiceWorker | EmployeeWorkerProductionConstruction | EconomicallyInactive | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
772 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 12.0 | กำไรสุทธิจากการทำการเกษตร | Net profits from farming | 2556.0 | 12469.0 | 0.0 | 16750.0 | 179.0 | 396.0 | 0.0 | 77.0 | 396.0 | 94.0 | 176.0 |
773 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 13.0 | เงินที่ได้รับเป็นการช่วยเหลือ | From current transfers | 2556.0 | 501.0 | 0.0 | 300.0 | 607.0 | 431.0 | 257.0 | 150.0 | 254.0 | 287.0 | 6877.0 |
774 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 14.0 | รายได้จากทรัพย์สิน | From property income | 2556.0 | 0.0 | 0.0 | 0.0 | 12.0 | 47.0 | 0.0 | 0.0 | 9.0 | 0.0 | 34.0 |
775 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 15.0 | รายได้ที่ไม่เป็นตัวเงิน | Non-money income | 2556.0 | 2191.0 | 0.0 | 2042.0 | 3127.0 | 2571.0 | 2204.0 | 1855.0 | 2167.0 | 1766.0 | 2587.0 |
776 | 96 | นราธิวาส | Narathiwat | 4.0 | ภาคตะวันออกเฉียงเหนือ | Northeastern | 16.0 | รายได้ไม่ประจำ (ที่เป็นตัวเงิน) | Non - current money income | 2556.0 | 0.0 | 0.0 | 0.0 | 3.0 | 0.0 | 0.0 | 70.0 | 0.0 | 0.0 | 0.0 |
ตรวจเช็คชื่อคอลัมน์ (เนื่องจากบางทีจะมีเว้นวรรคจะทำให้ต้องพิมพ์เว้นวรรคตามไปด้วย)
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 777 entries, 0 to 776
Data columns (total 20 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 ProvinceKey 777 non-null object
1 ProvinceName 777 non-null object
2 ProvinceNameEng 777 non-null object
3 RegionKey 777 non-null float64
4 Region 777 non-null object
5 RegionEng 777 non-null object
6 SourceOfIncomeKey 777 non-null float64
7 SourceOfIncome 777 non-null object
8 SourceOfIncomeEng 777 non-null object
9 Year 777 non-null float64
10 FarmCulturePlantAnimalMainlyOwningLand 777 non-null float64
11 FarmCulturePlantAnimalMainlyRentingLand 777 non-null float64
12 FarmCultureFishingForestryHuntingAgri 777 non-null float64
13 EntrepreneursNonAgriBusiness 777 non-null float64
14 EmployeeTechnicianManager 777 non-null float64
15 EmployeeLabourInAgriForestryFishery 777 non-null float64
16 EmployeeLabourInLogisticTransportation 777 non-null float64
17 EmployeeClericalSalesServiceWorker 777 non-null float64
18 EmployeeWorkerProductionConstruction 777 non-null float64
19 EconomicallyInactive 768 non-null float64
dtypes: float64(13), object(7)
memory usage: 127.5+ KB
เนื่องจากประเทศไทยเป็นประเทศเกษตรกรรม เราอาจจะอยากรู้ว่ารายได้จากการเกษตรเป็นอย่างไร
จากคอลัมน์ข้างบน จะเห็นว่ามีคอลัมน์เกี่ยวกับการเกษตรอยู่สามคอลัมน์ คือ
FarmCulturePlantAnimalMainlyOwningLand
หรือ เกษตรกรรมโดยที่เป็นเจ้าของที่ดินFarmCulturePlantAnimalMainlyRentingLand
หรือ เกษตรกรรมโดยเช่าที่ดินFarmCultureFishingForestryHuntingAgri
หรือ เกษตรกรรมด้วยการเก็บเกี่ยวธรรมชาติ
เราสามารถสร้างคอลัมน์สำหรับรายได้รวมด้านการเกษตร
df["FarmCulture"] = df['FarmCulturePlantAnimalMainlyOwningLand'] + df['FarmCulturePlantAnimalMainlyRentingLand'] + df['FarmCultureFishingForestryHuntingAgri']
df.head()
ProvinceKey | ProvinceName | ProvinceNameEng | RegionKey | Region | RegionEng | SourceOfIncomeKey | SourceOfIncome | SourceOfIncomeEng | Year | ... | FarmCulturePlantAnimalMainlyRentingLand | FarmCultureFishingForestryHuntingAgri | EntrepreneursNonAgriBusiness | EmployeeTechnicianManager | EmployeeLabourInAgriForestryFishery | EmployeeLabourInLogisticTransportation | EmployeeClericalSalesServiceWorker | EmployeeWorkerProductionConstruction | EconomicallyInactive | FarmCulture | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 10.0 | ค่าจ้างและเงินเดือน | Wages and salaries | 2556.0 | ... | 0.0 | 4828.0 | 2599.0 | 49270.0 | 9500.0 | 14987.0 | 21842.0 | 21839.0 | 788.0 | 4828.0 |
1 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 11.0 | กำไรสุทธิจากการทำธุรกิจ | Net profits from business | 2556.0 | ... | 0.0 | 15074.0 | 22155.0 | 4761.0 | 0.0 | 0.0 | 827.0 | 499.0 | 235.0 | 15074.0 |
2 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 12.0 | กำไรสุทธิจากการทำการเกษตร | Net profits from farming | 2556.0 | ... | 0.0 | 25736.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 33782.0 |
3 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 13.0 | เงินที่ได้รับเป็นการช่วยเหลือ | From current transfers | 2556.0 | ... | 0.0 | 403.0 | 334.0 | 511.0 | 200.0 | 0.0 | 374.0 | 346.0 | 14925.0 | 853.0 |
4 | 11 | สมุทรปราการ | Samut Prakarn | 5.0 | ภาคใต้ | Southern | 14.0 | รายได้จากทรัพย์สิน | From property income | 2556.0 | ... | 0.0 | 93.0 | 116.0 | 726.0 | 0.0 | 0.0 | 128.0 | 2.0 | 1009.0 | 104.0 |
5 rows × 21 columns
จะเห็นว่าในตารางจะมีคอลัมน์ใหม่ชื่อ FarmCulture
ที่เราสร้างขึ้นมา
เราสามารถดูสถิติเบื้องต้นได้ด้วย
df["FarmCulture"].describe()
count 777.000000
mean 8973.027156
std 19083.362443
min -8073.000000
25% 376.000000
50% 2604.000000
75% 8072.000000
max 290563.000000
Name: FarmCulture, dtype: float64
1.1) คำสั่ง groupby ในการจับกลุ่ม
เราสามารถแบ่งกลุ่มจังหวัดตามภาคตามคอลัมน์ Region
ได้ด้วยคำสั่ง .groupby()
df.groupby("Region").groups
{'กรุงเทพมหานคร': [77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, ...], 'ภาคกลาง': [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, ...], 'ภาคตะวันออกเฉียงเหนือ': [126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, ...], 'ภาคเหนือ': [7, 8, 9, 10, 11, 12, 13, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, ...], 'ภาคใต้': [0, 1, 2, 3, 4, 5, 6, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 105, 106, 107, 108, 109, 110, 111, 168, 169, 170, 171, 172, 173, 174, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, ...]}
โดยผลลัพธ์จะบอกว่าแต่ละกลุ่มประกอบด้วยแถวอะไรบ้าง
และเราสามารถหาผลรวมของแต่ละแถวในกลุ่มนั้น ๆ ได้เลย
df.groupby("Region").sum()
RegionKey | SourceOfIncomeKey | Year | FarmCulturePlantAnimalMainlyOwningLand | FarmCulturePlantAnimalMainlyRentingLand | FarmCultureFishingForestryHuntingAgri | EntrepreneursNonAgriBusiness | EmployeeTechnicianManager | EmployeeLabourInAgriForestryFishery | EmployeeLabourInLogisticTransportation | EmployeeClericalSalesServiceWorker | EmployeeWorkerProductionConstruction | EconomicallyInactive | FarmCulture | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Region | ||||||||||||||
กรุงเทพมหานคร | 189.0 | 2457.0 | 483287.0 | 493227.0 | 460342.1 | 246300.0 | 704046.3 | 1141435.7 | 379871.2 | 388438.2 | 532337.0 | 427590.3 | 364729.2 | 1199869.1 |
ภาคกลาง | 406.0 | 2639.0 | 519078.0 | 477350.2 | 604429.6 | 506257.9 | 864561.2 | 1333593.5 | 366497.8 | 427596.5 | 592936.9 | 452768.3 | 398528.1 | 1588037.7 |
ภาคตะวันออกเฉียงเหนือ | 644.0 | 2093.0 | 411656.0 | 622259.3 | 396864.3 | 514714.3 | 839254.8 | 1031391.8 | 361313.5 | 372498.3 | 532112.5 | 435326.9 | 410296.7 | 1533837.9 |
ภาคเหนือ | 315.0 | 1365.0 | 268520.0 | 428922.6 | 489220.3 | 194687.0 | 506492.4 | 830778.1 | 203331.6 | 251573.1 | 377324.3 | 383757.1 | 253377.2 | 1112829.9 |
ภาคใต้ | 595.0 | 1547.0 | 304304.0 | 622516.5 | 620510.0 | 294441.0 | 508235.0 | 788200.4 | 191387.1 | 276685.4 | 375069.9 | 379316.8 | 284316.9 | 1537467.5 |
สังเกตว่าผลลัพธ์จากคำสั่ง .sum()
ก็จะได้ออกมาเป็น DataFrame
เช่นกัน
.sum()
และนำไปใช้งานได้ (ข้อนี้มี explanation)
หากเราสนใจเพียงคอลัมน์การเกษตรก็สามารถระบุชื่อ FarmCulture
ได้
df.groupby("Region").sum()["FarmCulture"]
Region
กรุงเทพมหานคร 1199869.1
ภาคกลาง 1588037.7
ภาคตะวันออกเฉียงเหนือ 1533837.9
ภาคเหนือ 1112829.9
ภาคใต้ 1537467.5
Name: FarmCulture, dtype: float64
หากต้องการตารางดังด้านล่าง จะต้องใช้คำสั่งอะไร HINT: เป็นค่าเฉลี่ย
ProvinceName
กระบี่ 10778.571429
กาฬสินธุ์ 5200.214286
กำแพงเพชร 5749.357143
ขอนแก่น 11791.585714
จันทบุรี 27671.500000
ฉะเชิงเทรา 17586.428571
ชลบุรี 13317.785714
ชัยนาท 13625.785714
ชัยภูมิ 10499.214286
ชุมพร 12244.857143
ตรัง 4952.571429
ตราด 8775.642857
ตาก 5005.142857
นครนายก 6943.928571
นครพนม 5240.357143
นครราชสีมา 9564.271429
นครศรีธรรมราช 15304.092857
นครสวรรค์ 7893.857143
นนทบุรี 9439.785714
นราธิวาส 5496.142857
น่าน 5156.571429
บึงกาฬ 9337.571429
ปทุมธานี 9495.357143
ปัตตานี 6626.785714
พระนครศรีอยุธยา 14260.285714
พะเยา 5927.142857
พังงา 8671.642857
พัทลุง 8313.285714
พิจิตร 8698.214286
พิษณุโลก 7091.928571
ภูเก็ต 8890.214286
มหาสารคาม 6917.142857
มุกดาหาร 9913.571429
ยะลา 6514.000000
ยโสธร 8425.785714
ระนอง 8717.285714
ระยอง 15603.821429
ร้อยเอ็ด 5925.714286
ลพบุรี 6224.571429
ลำพูน 6302.476190
สกลนคร 5314.071429
สงขลา 10315.400000
สตูล 11910.571429
สมุทรปราการ 3132.809524
สระบุรี 6558.128571
สระแก้ว 15220.785714
สิงห์บุรี 14567.214286
สุราษฎร์ธานี 16682.428571
สุรินทร์ 8360.142857
สุโขทัย 9951.142857
หนองคาย 5650.571429
หนองบัวลำภู 7091.285714
อุดรธานี 9947.728571
อุตรดิตถ์ 7087.714286
อุบลราชธานี 7929.900000
อ่างทอง 8595.785714
เชียงราย 4424.928571
เชียงใหม่ 6224.257143
เพชรบูรณ์ 7933.071429
แม่ฮ่องสอน 3047.035714
Name: FarmCulture, dtype: float64
2) Visualization
หากอ่านตัวเลขด้านบนแล้วเรายังไม่เห็นภาพรวมของแต่ละจังหวัด เราก็สามารถพลอตกราฟได้
import plotly.express as px
สร้าง dataframe ชื่อ mean_prov
ที่เป็นค่าเฉลี่ยของแต่ละจังหวัด
mean_prov = df.groupby("ProvinceName").mean()
mean_prov
RegionKey | SourceOfIncomeKey | Year | FarmCulturePlantAnimalMainlyOwningLand | FarmCulturePlantAnimalMainlyRentingLand | FarmCultureFishingForestryHuntingAgri | EntrepreneursNonAgriBusiness | EmployeeTechnicianManager | EmployeeLabourInAgriForestryFishery | EmployeeLabourInLogisticTransportation | EmployeeClericalSalesServiceWorker | EmployeeWorkerProductionConstruction | EconomicallyInactive | FarmCulture | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ProvinceName | ||||||||||||||
กระบี่ | 4.0 | 13.0 | 2556.0 | 4428.285714 | 2668.857143 | 3681.428571 | 4284.714286 | 6420.714286 | 2313.857143 | 1996.428571 | 3249.285714 | 3077.285714 | 1978.571429 | 10778.571429 |
กาฬสินธุ์ | 2.0 | 13.0 | 2557.0 | 1738.357143 | 2324.214286 | 1137.642857 | 2846.428571 | 6287.714286 | 894.357143 | 1906.142857 | 2602.000000 | 2075.571429 | 1365.000000 | 5200.214286 |
กำแพงเพชร | 1.0 | 13.0 | 2556.5 | 2355.214286 | 2370.142857 | 1024.000000 | 3561.285714 | 4334.928571 | 1374.500000 | 1624.714286 | 1771.285714 | 1909.214286 | 1768.857143 | 5749.357143 |
ขอนแก่น | 2.0 | 13.0 | 2558.0 | 2559.171429 | 7637.914286 | 1594.500000 | 3753.428571 | 6470.657143 | 2000.571429 | 2380.900000 | 2862.985714 | 2580.900000 | 1809.828571 | 11791.585714 |
จันทบุรี | 5.0 | 13.0 | 2557.0 | 7216.714286 | 18771.214286 | 1683.571429 | 4087.000000 | 2632.214286 | 1059.642857 | 1261.714286 | 1671.500000 | 1484.642857 | 2442.071429 | 27671.500000 |
ฉะเชิงเทรา | 5.0 | 13.0 | 2557.0 | 11481.571429 | 4003.428571 | 2101.428571 | 4299.357143 | 8631.214286 | 1680.642857 | 3100.214286 | 3798.500000 | 4110.785714 | 3093.928571 | 17586.428571 |
ชลบุรี | 5.0 | 13.0 | 2557.0 | 6572.785714 | 4588.428571 | 2156.571429 | 4973.642857 | 4149.071429 | 1965.928571 | 2981.071429 | 3696.857143 | 3659.857143 | 2595.857143 | 13317.785714 |
ชัยนาท | 3.0 | 13.0 | 2557.0 | 4278.071429 | 4151.428571 | 5196.285714 | 6755.214286 | 7764.571429 | 2363.071429 | 1663.357143 | 2966.142857 | 2401.285714 | 1637.214286 | 13625.785714 |
ชัยภูมิ | 2.0 | 13.0 | 2557.0 | 2581.500000 | 2942.357143 | 4975.357143 | 5714.000000 | 6848.714286 | 1286.357143 | 2429.785714 | 2773.214286 | 2232.928571 | 1997.142857 | 10499.214286 |
ชุมพร | 4.0 | 13.0 | 2558.0 | 3873.857143 | 3494.714286 | 4876.285714 | 5983.000000 | 7716.714286 | 2725.000000 | 1321.714286 | 3512.000000 | 2813.571429 | 2726.571429 | 12244.857143 |
ตรัง | 4.0 | 13.0 | 2557.0 | 3337.785714 | 0.000000 | 1614.785714 | 13530.785714 | 9291.571429 | 2639.642857 | 2665.000000 | 4304.428571 | 4037.000000 | 2679.785714 | 4952.571429 |
ตราด | 5.0 | 13.0 | 2557.0 | 4043.857143 | 2388.714286 | 2343.071429 | 4642.785714 | 6896.428571 | 1911.642857 | 2045.642857 | 2910.857143 | 2066.642857 | 2111.357143 | 8775.642857 |
ตาก | 1.0 | 13.0 | 2557.0 | 2312.785714 | 1629.071429 | 1063.285714 | 3748.428571 | 5976.428571 | 1367.142857 | 1560.214286 | 2967.500000 | 2319.571429 | 1854.500000 | 5005.142857 |
นครนายก | 5.0 | 13.0 | 2557.0 | 3179.571429 | 3180.071429 | 584.285714 | 4298.571429 | 8028.928571 | 1401.500000 | 2026.428571 | 2738.214286 | 2981.571429 | 1851.571429 | 6943.928571 |
นครพนม | 2.0 | 13.0 | 2557.0 | 1745.785714 | 1988.714286 | 1505.857143 | 2641.071429 | 5885.428571 | 2010.214286 | 1451.285714 | 2619.785714 | 1929.642857 | 1535.785714 | 5240.357143 |
นครราชสีมา | 2.0 | 13.0 | 2557.0 | 2679.014286 | 5178.607143 | 1706.650000 | 4212.878571 | 7564.214286 | 2734.407143 | 2272.592857 | 3347.078571 | 3327.871429 | 2483.035714 | 9564.271429 |
นครศรีธรรมราช | 4.0 | 13.0 | 2557.0 | 4546.114286 | 4011.521429 | 6746.457143 | 5023.378571 | 7159.985714 | 2498.892857 | 1813.764286 | 2924.878571 | 2997.650000 | 2401.914286 | 15304.092857 |
นครสวรรค์ | 1.0 | 13.0 | 2557.0 | 3446.857143 | 3012.071429 | 1434.928571 | 5703.571429 | 5688.214286 | 1654.071429 | 2166.142857 | 2812.500000 | 2878.071429 | 1859.857143 | 7893.857143 |
นนทบุรี | 3.0 | 13.0 | 2557.0 | 3239.214286 | 4528.857143 | 1671.714286 | 4342.571429 | 8283.714286 | 891.214286 | 3091.357143 | 3686.500000 | 3892.214286 | 3601.214286 | 9439.785714 |
นราธิวาส | 4.0 | 13.0 | 2556.0 | 2322.142857 | 0.000000 | 3174.000000 | 3443.000000 | 3966.714286 | 1752.285714 | 1426.000000 | 2253.714286 | 1434.285714 | 1466.000000 | 5496.142857 |
น่าน | 1.0 | 13.0 | 2557.0 | 2028.071429 | 1938.928571 | 1189.571429 | 4080.142857 | 5787.714286 | 2414.285714 | 1684.071429 | 3095.142857 | 1955.000000 | 2064.785714 | 5156.571429 |
บึงกาฬ | 2.0 | 13.0 | 2557.0 | 2975.214286 | 4029.428571 | 2332.928571 | 4148.000000 | 8981.428571 | 2225.714286 | 2030.571429 | 3363.571429 | 2263.571429 | 1762.857143 | 9337.571429 |
ปทุมธานี | 3.0 | 13.0 | 2557.0 | 5295.000000 | 4200.357143 | 0.000000 | 7658.000000 | 8149.428571 | 2111.714286 | 2901.642857 | 4576.142857 | 3772.500000 | 3805.214286 | 9495.357143 |
ปัตตานี | 4.0 | 13.0 | 2557.0 | 1874.500000 | 1305.714286 | 3446.571429 | 3118.214286 | 5411.142857 | 1873.357143 | 1870.928571 | 1980.714286 | 1872.785714 | 1984.357143 | 6626.785714 |
พระนครศรีอยุธยา | 3.0 | 13.0 | 2557.0 | 4244.142857 | 8706.357143 | 1309.785714 | 3269.928571 | 6927.571429 | 2915.928571 | 2716.428571 | 3296.714286 | 3976.857143 | 2174.142857 | 14260.285714 |
พะเยา | 1.0 | 13.0 | 2558.0 | 1915.000000 | 2487.714286 | 1524.428571 | 3950.571429 | 5862.571429 | 0.000000 | 2617.285714 | 2885.000000 | 2134.714286 | 1678.000000 | 5927.142857 |
พังงา | 4.0 | 13.0 | 2557.0 | 4526.000000 | 1533.357143 | 2612.285714 | 5207.928571 | 5934.428571 | 2305.928571 | 2136.785714 | 3322.857143 | 2249.357143 | 2976.000000 | 8671.642857 |
พัทลุง | 4.0 | 13.0 | 2557.0 | 3670.000000 | 2869.142857 | 1774.142857 | 4096.285714 | 6846.857143 | 2227.285714 | 2441.214286 | 3187.571429 | 2449.000000 | 1745.214286 | 8313.285714 |
พิจิตร | 1.0 | 13.0 | 2557.0 | 3410.928571 | 3513.142857 | 1774.142857 | 3434.571429 | 8539.000000 | 1746.285714 | 2008.928571 | 2741.571429 | 2027.571429 | 2099.000000 | 8698.214286 |
พิษณุโลก | 1.0 | 13.0 | 2557.0 | 3016.571429 | 2877.142857 | 1198.214286 | 4682.214286 | 7177.928571 | 2745.857143 | 2655.214286 | 3361.571429 | 2513.714286 | 2275.642857 | 7091.928571 |
ภูเก็ต | 4.0 | 13.0 | 2557.0 | 3443.571429 | 4002.428571 | 1444.214286 | 5318.214286 | 6234.857143 | 2455.357143 | 2916.928571 | 4071.785714 | 3273.357143 | 3985.214286 | 8890.214286 |
มหาสารคาม | 2.0 | 13.0 | 2557.0 | 2161.357143 | 1449.071429 | 3306.714286 | 5689.428571 | 4212.785714 | 2144.785714 | 2344.285714 | 2332.428571 | 2016.071429 | 2091.416667 | 6917.142857 |
มุกดาหาร | 2.0 | 13.0 | 2558.0 | 2316.142857 | 6287.285714 | 1310.142857 | 7410.571429 | 6746.571429 | 1167.285714 | 2458.714286 | 3185.428571 | 2021.571429 | 1476.000000 | 9913.571429 |
ยะลา | 4.0 | 13.0 | 2556.0 | 2056.000000 | 0.000000 | 4458.000000 | 2596.857143 | 1427.571429 | 1366.857143 | 1628.714286 | 2955.142857 | 2199.142857 | 1542.000000 | 6514.000000 |
ยโสธร | 2.0 | 13.0 | 2557.0 | 2003.214286 | 1204.500000 | 5218.071429 | 3618.500000 | 5493.214286 | 2086.857143 | 1926.857143 | 2593.785714 | 1835.857143 | 1661.428571 | 8425.785714 |
ระนอง | 4.0 | 13.0 | 2557.0 | 4946.000000 | 0.000000 | 3771.285714 | 4137.714286 | 6007.928571 | 1694.642857 | 2166.285714 | 3286.357143 | 2157.428571 | 2869.571429 | 8717.285714 |
ระยอง | 5.0 | 13.0 | 2557.0 | 5397.607143 | 6220.500000 | 3985.714286 | 3771.857143 | 7404.671429 | 1785.792857 | 2289.314286 | 3522.921429 | 4763.342857 | 2487.921429 | 15603.821429 |
ร้อยเอ็ด | 2.0 | 13.0 | 2557.0 | 2148.500000 | 1025.000000 | 2752.214286 | 4135.571429 | 7556.142857 | 1775.071429 | 2550.714286 | 3300.071429 | 2107.714286 | 1851.928571 | 5925.714286 |
ลพบุรี | 3.0 | 13.0 | 2559.0 | 2748.785714 | 2204.285714 | 1271.500000 | 4059.142857 | 6902.785714 | 1716.214286 | 2075.857143 | 3763.714286 | 3003.428571 | 1801.642857 | 6224.571429 |
ลำพูน | 1.0 | 13.0 | 2558.0 | 2709.523810 | 2337.047619 | 1255.904762 | 4046.809524 | 7468.285714 | 3134.428571 | 1873.095238 | 3369.476190 | 3443.571429 | 2500.523810 | 6302.476190 |
สกลนคร | 2.0 | 13.0 | 2556.0 | 1916.071429 | 2654.500000 | 743.500000 | 3704.714286 | 6360.642857 | 1451.571429 | 1513.071429 | 2666.000000 | 1780.428571 | 1637.428571 | 5314.071429 |
สงขลา | 4.0 | 13.0 | 2557.0 | 4946.335714 | 1986.642857 | 3382.421429 | 4138.035714 | 6621.428571 | 2271.785714 | 2812.042857 | 3687.014286 | 3427.342857 | 2969.350000 | 10315.400000 |
สตูล | 4.0 | 13.0 | 2557.0 | 3032.785714 | 6679.000000 | 2198.785714 | 4310.714286 | 6770.142857 | 2194.357143 | 3368.142857 | 3584.142857 | 2654.857143 | 2035.928571 | 11910.571429 |
สมุทรปราการ | 5.0 | 13.0 | 2558.0 | 782.761905 | 0.000000 | 2350.047619 | 3885.666667 | 7938.857143 | 1014.095238 | 2115.809524 | 3575.000000 | 3329.571429 | 3162.857143 | 3132.809524 |
สระบุรี | 3.0 | 13.0 | 2558.0 | 3031.085714 | 3527.042857 | 0.000000 | 4356.342857 | 6305.871429 | 1650.942857 | 1816.871429 | 3767.471429 | 4964.014286 | 2612.171429 | 6558.128571 |
สระแก้ว | 5.0 | 13.0 | 2557.0 | 5399.214286 | 5169.785714 | 4651.785714 | 4400.785714 | 6649.214286 | 2344.214286 | 2885.142857 | 3089.357143 | 3032.857143 | 2279.285714 | 15220.785714 |
สิงห์บุรี | 3.0 | 13.0 | 2557.0 | 6077.571429 | 5139.428571 | 3350.214286 | 4220.928571 | 9004.000000 | 1654.214286 | 2319.642857 | 3308.500000 | 3616.285714 | 1831.642857 | 14567.214286 |
สุราษฎร์ธานี | 4.0 | 13.0 | 2556.0 | 7567.714286 | 5755.714286 | 3359.000000 | 5823.428571 | 7253.285714 | 3135.714286 | 2459.000000 | 3346.428571 | 2427.714286 | 3606.000000 | 16682.428571 |
สุรินทร์ | 2.0 | 13.0 | 2557.0 | 2799.642857 | 3775.500000 | 1785.000000 | 3637.571429 | 6598.428571 | 2424.142857 | 1872.642857 | 3021.857143 | 2198.142857 | 2413.000000 | 8360.142857 |
สุโขทัย | 1.0 | 13.0 | 2556.0 | 3841.142857 | 5226.142857 | 883.857143 | 4307.428571 | 7059.142857 | 1701.000000 | 1741.571429 | 3136.428571 | 1782.571429 | 2075.142857 | 9951.142857 |
หนองคาย | 2.0 | 13.0 | 2558.0 | 2129.142857 | 2080.428571 | 1441.000000 | 4302.571429 | 7936.571429 | 2061.571429 | 2502.000000 | 3313.428571 | 2147.571429 | 2106.714286 | 5650.571429 |
หนองบัวลำภู | 2.0 | 13.0 | 2557.0 | 2408.214286 | 2460.928571 | 2222.142857 | 3384.857143 | 4975.785714 | 1357.500000 | 2660.000000 | 3043.928571 | 2443.285714 | 2375.928571 | 7091.285714 |
อุดรธานี | 2.0 | 13.0 | 2557.0 | 2905.700000 | 4190.171429 | 2851.857143 | 6456.878571 | 7257.800000 | 1408.650000 | 2039.900000 | 2892.678571 | 2435.735714 | 2564.428571 | 9947.728571 |
อุตรดิตถ์ | 1.0 | 13.0 | 2557.0 | 2829.928571 | 2662.785714 | 1595.000000 | 3490.071429 | 6554.142857 | 2438.285714 | 2294.500000 | 3107.214286 | 2283.500000 | 1895.428571 | 7087.714286 |
อุบลราชธานี | 2.0 | 13.0 | 2557.0 | 2531.642857 | 1947.735714 | 3450.521429 | 3831.185714 | 6657.478571 | 1764.071429 | 1873.950000 | 3115.314286 | 2318.750000 | 2329.414286 | 7929.900000 |
อ่างทอง | 3.0 | 13.0 | 2557.0 | 3239.000000 | 4250.071429 | 1106.714286 | 3694.071429 | 9156.285714 | 2045.857143 | 2292.785714 | 3470.285714 | 4266.642857 | 1941.214286 | 8595.785714 |
เชียงราย | 1.0 | 13.0 | 2557.0 | 1615.142857 | 1750.928571 | 1058.857143 | 2344.714286 | 4883.142857 | 1174.428571 | 1753.785714 | 2213.857143 | 1854.857143 | 1604.000000 | 4424.928571 |
เชียงใหม่ | 1.0 | 13.0 | 2557.0 | 3202.414286 | 1722.571429 | 1299.271429 | 2767.107143 | 4134.921429 | 1666.157143 | 2453.185714 | 2397.371429 | 1827.914286 | 1451.228571 | 6224.257143 |
เพชรบูรณ์ | 1.0 | 13.0 | 2557.0 | 2957.357143 | 3097.928571 | 1877.785714 | 3492.928571 | 6546.500000 | 1849.714286 | 3268.857143 | 2866.785714 | 2262.571429 | 2514.071429 | 7933.071429 |
แม่ฮ่องสอน | 1.0 | 13.0 | 2557.0 | 1112.871429 | 944.364286 | 989.800000 | 2784.771429 | 4244.914286 | 3150.785714 | 1286.900000 | 2624.342857 | 1586.178571 | 1037.357143 | 3047.035714 |
สร้าง histogram สำหรับค่าเฉลี่ยของคอลัมน์ FarmCulture
ในแต่ละจังหวัดได้ดังนี้
px.histogram(mean_prov, x="FarmCulture")
หรือจะใช้คำสั่ง .hist()
ใน pandas เลยก็ได้ดังนี้
การพลอตกราฟด้วย pandas
จริงๆ แล้วใช้ matplotlib
เป็นเบื้องหลังในการพลอต
แต่ในคลาสนี้อ.เลือกที่จะสอน plotly.express
แทน matplotlib
เพราะคิดว่าสามารถเข้าใจ syntax ได้ง่ายมากกว่า
mean_prov["FarmCulture"].hist()
ทั้งนี้ เราสามารถกำหนดวิธีการพลอตกราฟด้วย pandas
ให้ใช้ plotly
ได้เช่นกัน
pd.options.plotting.backend = "plotly"
mean_prov["FarmCulture"].hist()
เราสามารถสร้าง histogram สำหรับคอลัมน์ EntrepreneursNonAgriBusiness
ได้ในทำนองเดียวกัน
ซึ่งเดาจากชื่อแล้วน่าจะเป็นสิ่งที่ไม่ใช่เกษตรกรรม (NonAgri)
px.histogram(mean_prov, x="EntrepreneursNonAgriBusiness")
หากเราต้องการนำสองค่ามาพลอตร่วมกันสามารถระบุได้ดังนี้
px.histogram(mean_prov, x=["FarmCulture","EntrepreneursNonAgriBusiness"])
4000-5990
มีกี่จังหวัดที่ค่า EntrepreneursNonAgriBusiness
สูงกว่า FarmCulture
นักเรียนสามารถกด tab เพื่อดู parameter ต่าง ๆ ที่สามารถระบุในฟังก์ชันนั้น ๆ ได้ (autocomplete)
ให้นักเรียนศึกษาตัวอย่างการสร้าง histogram แบบต่าง ๆ ที่ https://plotly.com/python/histograms/ เพื่อสร้างกราฟดังรูปข้างล่างที่ลักษณะคล้ายๆกัน
HINT: ใช้ mean_prov
เป็นข้อมูลเริ่มต้น