File size: 4,940 Bytes
7262190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
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
168
169
170
171
172
173
174
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0faa43fd",
   "metadata": {},
   "source": [
    "# Global Biodiversity Occurrences\n",
    "\n",
    "Starter notebook for the Kaggle dataset in this folder. It loads `occurrences.csv` with pandas and walks through quality checks and a first piece of analysis.\n",
    "\n",
    "**Files:** `occurrences.csv`, `data_dictionary.csv`."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0000f7f2",
   "metadata": {},
   "source": [
    "## 1. Load the data"
   ]
  },
  {
   "cell_type": "code",
   "id": "eab6c3a0",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv(\"occurrences.csv\", low_memory=False)\n",
    "print(df.shape)\n",
    "df[[\"scientificName\", \"kingdom\", \"country_name\", \"decimalLatitude\", \"year\"]].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "325de123",
   "metadata": {},
   "source": [
    "## 2. Shape, types and missing values"
   ]
  },
  {
   "cell_type": "code",
   "id": "e8f80bc1",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "print(f\"{df.shape[0]:,} rows x {df.shape[1]} columns\")\n",
    "\n",
    "missing = df.isna().mean().mul(100).sort_values(ascending=False)\n",
    "missing[missing > 0].round(1).to_frame(\"missing_%\").head(15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d963fe",
   "metadata": {},
   "source": [
    "## 3. Numeric summary"
   ]
  },
  {
   "cell_type": "code",
   "id": "aa2f8fd4",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.describe(include='number').T.round(2).head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8510caed",
   "metadata": {},
   "source": [
    "## 4. Records per kingdom"
   ]
  },
  {
   "cell_type": "code",
   "id": "efe00995",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df[\"kingdom\"].value_counts().head(10).plot.bar(figsize=(8, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "910b4588",
   "metadata": {},
   "source": [
    "## 5. Latitudinal diversity gradient\n",
    "\n",
    "Records per 5-degree band of absolute latitude."
   ]
  },
  {
   "cell_type": "code",
   "id": "45b88855",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "bins = range(0, 95, 5)\n",
    "band = pd.cut(df[\"abs_latitude\"], bins=bins)\n",
    "band.value_counts().sort_index().plot.bar(figsize=(11, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "208187ad",
   "metadata": {},
   "source": [
    "## 6. Sampling bias between countries"
   ]
  },
  {
   "cell_type": "code",
   "id": "94fab4d2",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df.groupby(\"country_name\").agg(records=(\"key\", \"count\"),\n",
    "                                    species=(\"species\", \"nunique\"),\n",
    "                                    families=(\"family\", \"nunique\")).sort_values(\"records\", ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35fe546b",
   "metadata": {},
   "source": [
    "## 7. Data quality flags"
   ]
  },
  {
   "cell_type": "code",
   "id": "47f65221",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df[\"n_issues\"].value_counts().sort_index().plot.bar(figsize=(8, 4))\n",
    "df[\"has_issues\"].value_counts(normalize=True).round(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e682571b",
   "metadata": {},
   "source": [
    "## 8. Record type mix by region"
   ]
  },
  {
   "cell_type": "code",
   "id": "c01dc0a0",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "(df.groupby([\"region\", \"basis_group\"]).size()\n",
    "   .unstack(fill_value=0).pipe(lambda t: t.div(t.sum(axis=1), axis=0) * 100).round(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aacabb11",
   "metadata": {},
   "source": [
    "## 9. Observation years"
   ]
  },
  {
   "cell_type": "code",
   "id": "fb209d5e",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "df[\"year\"].dropna().astype(int).plot.hist(bins=40, figsize=(10, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e43770c",
   "metadata": {},
   "source": [
    "## Next steps\n",
    "\n",
    "See `KAGGLE.md` for analysis ideas, caveats and provenance."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11",
   "mimetype": "text/x-python",
   "file_extension": ".py",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}