2023年9月29日 星期五

用Python以及ChaptGPT來分析虎尾鎮111年度的農產品

1.資料來源:農情報告資料網

2.選擇"一般作物查詢"的"各項作物種植面積查詢(個別縣市及鄉鎮)"選項。

3.如上圖,按下"送出"鈕,可以得到下列資料:

4.將上圖有關於虎尾鎮農作物的資訊全部選擇起來,如下圖:

5.複製上圖的資料給ChatGPT,可以得到下列資料。


6.下達"用表格整理"的指令。


7.下達指令"給我 Python程式能整理成表格"


8.ChatGPT產生出來的程式,因資料集較多,會以簡化方式呈現。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import pandas as pd

# 資料
data = [
    ("雜糧類", "合計", 4120.43),
    ("雜糧類", "落花生", 1304.03),
    ("雜糧類", "食用玉米", 917.76),
    ("雜糧類", "甘藷", 87.85),
    # ... 其他作物資料
]

# 建立DataFrame
df = pd.DataFrame(data, columns=["類別", "作物", "面積 (公頃)"])

# 顯示DataFrame
print(df)

9.可以下達"請給我完整資料集的程式"

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
import pandas as pd

data = [
    ("雜糧類", "合計", 4120.43),
    ("雜糧類", "落花生", 1304.03),
    ("雜糧類", "食用玉米", 917.76),
    ("雜糧類", "甘藷", 87.85),
    ("雜糧類", "蜀黍高粱", 21.33),
    ("雜糧類", "綠豆", 4.30),
    ("雜糧類", "大豆", 4.15),
    ("雜糧類", "其他豆類", 0.35),
    ("蔬菜類", "合計", 1520.67),
    ("蔬菜類", "馬鈴薯", 451.09),
    ("蔬菜類", "蒜頭", 400.78),
    ("蔬菜類", "甘藍", 135.06),
    ("蔬菜類", "蔥", 92.26),
    ("蔬菜類", "食用番茄", 55.85),
    ("蔬菜類", "不結球白菜", 55.55),
    ("蔬菜類", "青花菜", 46.03),
    ("蔬菜類", "萵苣", 36.42),
    ("蔬菜類", "芹菜", 31.65),
    ("蔬菜類", "其他葉菜", 26.75),
    ("蔬菜類", "結球白菜", 22.40),
    ("蔬菜類", "洋香瓜", 22.30),
    ("蔬菜類", "甜椒", 20.86),
    ("蔬菜類", "南瓜", 19.55),
    ("蔬菜類", "西瓜", 19.32),
    ("蔬菜類", "越瓜", 12.35),
    ("蔬菜類", "胡瓜", 10.42),
    ("蔬菜類", "香瓜", 10.36),
    ("蔬菜類", "芥藍", 7.20),
    ("蔬菜類", "大芥菜", 6.10),
    ("蔬菜類", "節瓜毛瓜", 5.95),
    ("蔬菜類", "扁蒲", 5.25),
    ("蔬菜類", "辣椒", 4.40),
    ("蔬菜類", "菠菜", 4.20),
    ("蔬菜類", "蘆筍", 3.84),
    ("蔬菜類", "竹筍", 2.50),
    ("蔬菜類", "芫荽", 1.85),
    ("蔬菜類", "茄子", 1.80),
    ("蔬菜類", "其他果菜", 1.50),
    ("蔬菜類", "苦瓜", 1.15),
    ("蔬菜類", "毛豆", 1.10),
    ("蔬菜類", "香芹菜", 1.10),
    ("蔬菜類", "冬瓜", 1.00),
    ("蔬菜類", "胡蘿蔔", 0.70),
    ("蔬菜類", "結頭菜", 0.55),
    ("蔬菜類", "蘿蔔", 0.53),
    ("蔬菜類", "草莓", 0.30),
    ("蔬菜類", "蔥頭", 0.30),
    ("蔬菜類", "四季豆", 0.20),
    ("蔬菜類", "絲瓜", 0.15),
    ("果品類", "合計", 116.13),
    ("果品類", "香蕉", 60.05),
    ("果品類", "其他果樹", 16.73),
    ("果品類", "紅龍果", 8.22),
    ("果品類", "木瓜", 8.02),
    ("果品類", "改良種芒果", 6.50),
    ("果品類", "番石榴", 3.89),
    ("果品類", "柳橙", 3.00),
    ("果品類", "檸檬", 2.72),
    ("果品類", "酪梨", 2.10),
    ("果品類", "文旦柚", 1.40),
    ("果品類", "可可椰子", 0.90),
    ("果品類", "椪柑", 0.72),
    ("果品類", "百香果", 0.65),
    ("果品類", "荔枝", 0.50),
    ("果品類", "蓮霧", 0.48),
    ("果品類", "其他柑桔", 0.25),
    ("牧草類", "合計", 5.80),
    ("牧草類", "其他長期牧草", 5.40),
    ("牧草類", "狼尾草", 0.40),
    ("特用作物", "合計", 33.10),
    ("特用作物", "羅勒", 31.45),
    ("特用作物", "其他短期特作", 0.60),
    ("特用作物", "其他長期特作", 0.40),
    ("特用作物", "向日葵", 0.30),
    ("特用作物", "生食甘蔗", 0.25),
    ("特用作物", "荖葉", 0.10),
    ("藥用作物", "合計", 2.25),
    ("藥用作物", "其他藥用", 2.10),
    ("藥用作物", "薄荷", 0.15),
    ("花卉", "合計", 12.74),
    ("花卉", "洋桔梗", 5.45),
    ("花卉", "夜來香", 4.00),
    ("花卉", "蘭花", 1.67),
    ("花卉", "其他短期切花", 0.60),
    ("花卉", "菊花", 0.50),
    ("花卉", "玫瑰", 0.40),
    ("花卉", "文心蘭", 0.12),
    ("苗圃", "苗圃", 89.97),
    ("洋菇類", "洋菇", 1320.00),
    ("香菇及其他菇類", "備註", ""),
]

# 建立DataFrame
df = pd.DataFrame(data, columns=["類別", "作物", "面積 (公頃)"])

# 顯示DataFrame
print(df)

10.執行結果:

11.下達"每一項列出前三名的程式"指令

12.您應該會發現,資料集被ChatGPT給省略掉了,完整程式如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 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
 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
105
106
107
108
109
110
111
112
113
114
115
import pandas as pd

data = [
    ("雜糧類", "合計", 4120.43),
    ("雜糧類", "落花生", 1304.03),
    ("雜糧類", "食用玉米", 917.76),
    ("雜糧類", "甘藷", 87.85),
    ("雜糧類", "蜀黍高粱", 21.33),
    ("雜糧類", "綠豆", 4.30),
    ("雜糧類", "大豆", 4.15),
    ("雜糧類", "其他豆類", 0.35),
    ("蔬菜類", "合計", 1520.67),
    ("蔬菜類", "馬鈴薯", 451.09),
    ("蔬菜類", "蒜頭", 400.78),
    ("蔬菜類", "甘藍", 135.06),
    ("蔬菜類", "蔥", 92.26),
    ("蔬菜類", "食用番茄", 55.85),
    ("蔬菜類", "不結球白菜", 55.55),
    ("蔬菜類", "青花菜", 46.03),
    ("蔬菜類", "萵苣", 36.42),
    ("蔬菜類", "芹菜", 31.65),
    ("蔬菜類", "其他葉菜", 26.75),
    ("蔬菜類", "結球白菜", 22.40),
    ("蔬菜類", "洋香瓜", 22.30),
    ("蔬菜類", "甜椒", 20.86),
    ("蔬菜類", "南瓜", 19.55),
    ("蔬菜類", "西瓜", 19.32),
    ("蔬菜類", "越瓜", 12.35),
    ("蔬菜類", "胡瓜", 10.42),
    ("蔬菜類", "香瓜", 10.36),
    ("蔬菜類", "芥藍", 7.20),
    ("蔬菜類", "大芥菜", 6.10),
    ("蔬菜類", "節瓜毛瓜", 5.95),
    ("蔬菜類", "扁蒲", 5.25),
    ("蔬菜類", "辣椒", 4.40),
    ("蔬菜類", "菠菜", 4.20),
    ("蔬菜類", "蘆筍", 3.84),
    ("蔬菜類", "竹筍", 2.50),
    ("蔬菜類", "芫荽", 1.85),
    ("蔬菜類", "茄子", 1.80),
    ("蔬菜類", "其他果菜", 1.50),
    ("蔬菜類", "苦瓜", 1.15),
    ("蔬菜類", "毛豆", 1.10),
    ("蔬菜類", "香芹菜", 1.10),
    ("蔬菜類", "冬瓜", 1.00),
    ("蔬菜類", "胡蘿蔔", 0.70),
    ("蔬菜類", "結頭菜", 0.55),
    ("蔬菜類", "蘿蔔", 0.53),
    ("蔬菜類", "草莓", 0.30),
    ("蔬菜類", "蔥頭", 0.30),
    ("蔬菜類", "四季豆", 0.20),
    ("蔬菜類", "絲瓜", 0.15),
    ("果品類", "合計", 116.13),
    ("果品類", "香蕉", 60.05),
    ("果品類", "其他果樹", 16.73),
    ("果品類", "紅龍果", 8.22),
    ("果品類", "木瓜", 8.02),
    ("果品類", "改良種芒果", 6.50),
    ("果品類", "番石榴", 3.89),
    ("果品類", "柳橙", 3.00),
    ("果品類", "檸檬", 2.72),
    ("果品類", "酪梨", 2.10),
    ("果品類", "文旦柚", 1.40),
    ("果品類", "可可椰子", 0.90),
    ("果品類", "椪柑", 0.72),
    ("果品類", "百香果", 0.65),
    ("果品類", "荔枝", 0.50),
    ("果品類", "蓮霧", 0.48),
    ("果品類", "其他柑桔", 0.25),
    ("牧草類", "合計", 5.80),
    ("牧草類", "其他長期牧草", 5.40),
    ("牧草類", "狼尾草", 0.40),
    ("特用作物", "合計", 33.10),
    ("特用作物", "羅勒", 31.45),
    ("特用作物", "其他短期特作", 0.60),
    ("特用作物", "其他長期特作", 0.40),
    ("特用作物", "向日葵", 0.30),
    ("特用作物", "生食甘蔗", 0.25),
    ("特用作物", "荖葉", 0.10),
    ("藥用作物", "合計", 2.25),
    ("藥用作物", "其他藥用", 2.10),
    ("藥用作物", "薄荷", 0.15),
    ("花卉", "合計", 12.74),
    ("花卉", "洋桔梗", 5.45),
    ("花卉", "夜來香", 4.00),
    ("花卉", "蘭花", 1.67),
    ("花卉", "其他短期切花", 0.60),
    ("花卉", "菊花", 0.50),
    ("花卉", "玫瑰", 0.40),
    ("花卉", "文心蘭", 0.12),
    ("苗圃", "苗圃", 89.97),
    ("洋菇類", "洋菇", 1320.00),
    ("香菇及其他菇類", "備註", ""),
]

# 建立DataFrame
df = pd.DataFrame(data, columns=["類別", "作物", "面積 (公頃)"])

# 將資料按照類別和面積排序,降序排列
df_sorted = df.sort_values(by=["類別", "面積 (公頃)"], ascending=[True, False])

# 創建一個空的DataFrame,用於存儲前三名
top_three_df = pd.DataFrame(columns=["類別", "作物", "面積 (公頃)"])

# 找出每一類的前三名
categories = df_sorted["類別"].unique()
for category in categories:
    top_three = df_sorted[df_sorted["類別"] == category].head(3)
    top_three_df = pd.concat([top_three_df, top_three])

# 重設索引
top_three_df.reset_index(drop=True, inplace=True)

# 顯示前三名
print(top_three_df)

13.虎尾鎮前三名的農作物的執行結果


沒有留言:

張貼留言