-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython102_workshop_script.py
More file actions
179 lines (117 loc) · 3.75 KB
/
python102_workshop_script.py
File metadata and controls
179 lines (117 loc) · 3.75 KB
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
"""
Python Working with Data
Data download:
https://figshare.com/ndownloader/articles/1314459/versions/10
Adapted from Data Carpentry's material:
https://datacarpentry.org/python-ecology-lesson/02-starting-with-data.html
Accompanying slides:
https://docs.google.com/presentation/d/1ii8DKPjMA6krTikfbem7NLjA9XxeCf3e4rwD2N-yyKY/edit?usp=drive_link
"""
# Import library
import pandas as pd
# Load data
surveys_df = pd.read_csv("data/surveys.csv")
# -----------------------------
# Checking data types
# -----------------------------
type(surveys_df)
surveys_df.dtypes
surveys_df['sex'].dtype
surveys_df['record_id'].dtype
# Convert data types
surveys_df['record_id'] = surveys_df['record_id'].astype('float64')
surveys_df['record_id'].dtype
surveys_df['plot_id'].dtype
surveys_df['plot_id'] = surveys_df['plot_id'].astype("float")
surveys_df['plot_id'].dtype
# -----------------------------
# Remove rows with missing data
# -----------------------------
df_na = surveys_df.dropna()
df_na
df_na.to_csv('data/surveys_complete.csv', index=False)
# -----------------------------
# Determining object properties
# -----------------------------
surveys_df.head()
surveys_df.columns
surveys_df.shape
surveys_df.count()
# -----------------------------
# Statistics From Data
# -----------------------------
surveys_df.columns
pd.unique(surveys_df['species_id'])
surveys_df['weight'].describe()
surveys_df['weight'].min()
# or .max(), .mean(), .std(), .count()
# -----------------------------
# Group By
# -----------------------------
grouped_data = surveys_df.groupby('sex')
grouped_data
# (Exercise placeholder)
# How many recorded individuals are female (F) and male (M)?
grouped_data2 = surveys_df.groupby(['plot_id', 'sex'])
grouped_data2.mean(numeric_only=True)
# -----------------------------
# Creating Summary Counts
# -----------------------------
species_counts = surveys_df.groupby('species_id')['record_id'].count()
species_counts
surveys_df.groupby('species_id')['record_id'].count()['DO']
# -----------------------------
# Basic Plots
# -----------------------------
total_count = surveys_df.groupby('plot_id')['record_id'].nunique()
total_count.plot(kind='bar')
# -----------------------------
# Indexing, Slicing, Subsetting
# -----------------------------
# Column selection
surveys_df['species_id']
surveys_df.species_id
surveys_species = surveys_df['species_id']
surveys_df[['species_id', 'plot_id']]
# Row slicing
surveys_df[0:3]
surveys_df[:5]
surveys_df[-1:]
# -----------------------------
# Copying vs Referencing
# -----------------------------
true_copy_surveys_df = surveys_df.copy()
ref_surveys_df = surveys_df
ref_surveys_df[0:3] = 0
ref_surveys_df.head()
surveys_df.head()
# Reset dataframe
surveys_df = pd.read_csv("data/surveys.csv")
# -----------------------------
# loc and iloc
# -----------------------------
surveys_df.loc[[0, 10], :]
surveys_df.iloc[0:3, 1:4]
# -----------------------------
# Subsetting Using Criteria
# -----------------------------
surveys_df[surveys_df.year == 2002]
surveys_df[surveys_df.year != 2002]
surveys_df[(surveys_df.year >= 1980) & (surveys_df.year <= 1985)]
surveys_df[surveys_df['species_id'].isin(['NL'])]
# Opposite:
# surveys_df[~surveys_df['species_id'].isin(['NL'])]
# -----------------------------
# Masks / Null Values
# -----------------------------
surveys_df[pd.isnull(surveys_df).any(axis=1)]
empty_weights = surveys_df[pd.isnull(surveys_df['weight'])]
# -----------------------------
# Exercises (left as prompts)
# -----------------------------
# 1. Subset rows from 1999 with weight <= 8
# 2. Query weight >= 0
# 3. Find plots with 'NL' and 'DM'
# 4. Select rows where sex is NOT 'M' or 'F'
# 5. Create DataFrame with sex not male/female, assign 'x'
# 6. Create DataFrame with sex M/F and weight > 0