Skip to content

Commit a1e2a23

Browse files
20260827 - section headers and cross-references
1 parent 1bbdb82 commit a1e2a23

2 files changed

Lines changed: 36 additions & 36 deletions

File tree

dataManagement.qmd

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ format:
1212
toggle: true
1313
---
1414

15-
# Import Modules {#importModules}
1615
# Install Modules {#sec-installModules}
1716

1817
Terminal/command prompt:
@@ -62,7 +61,7 @@ import pandas as pd
6261
import matplotlib.pyplot as pp
6362
```
6463

65-
# Import Data {#importData}
64+
# Import Data {#sec-importData}
6665

6766
Importing data using `pandas` takes syntax of the following form for `.csv` files:
6867

@@ -86,7 +85,7 @@ mydata = pd.read_csv("https://osf.io/s6wrm/download") # uses the pandas module
8685
mydata = pd.read_csv("data/titanic.csv") #https://osf.io/s6wrm/download
8786
```
8887

89-
# Save Data {#saveData}
88+
# Save Data {#sec-saveData}
9089

9190
Saving data in Python takes syntax of the following form for `.csv` files:
9291

@@ -104,15 +103,15 @@ For example:
104103
mydata.to_csv("mydata.csv", index = False)
105104
```
106105

107-
# Set a Seed {#seed}
106+
# Set a Seed {#sec-seed}
108107

109108
Set a seed (any number) to reproduce the results of analyses that involve random number generation.
110109

111110
```{python}
112111
random.seed(52242) # uses the random module
113112
```
114113

115-
# Run a `Python` Script {#runScript}
114+
# Run a `Python` Script {#sec-runScript}
116115

117116
To run a `Python` script, use the following syntax:
118117

@@ -122,7 +121,7 @@ To run a `Python` script, use the following syntax:
122121
%run "filepath/filename.py"
123122
```
124123

125-
# Render a Quarto (`.qmd`) File {#renderQmd}
124+
# Render a Quarto (`.qmd`) File {#sec-renderQmd}
126125

127126
To render a Quarto (`.qmd`) file, you would typically use the command line.
128127
Here is the equivalent command in a `Python` cell using the `!` operator to run shell commands:
@@ -133,15 +132,15 @@ Here is the equivalent command in a `Python` cell using the `!` operator to run
133132
!quarto render "filepath/filename.qmd"
134133
```
135134

136-
# Variable Names {#varNames}
135+
# Variable Names {#sec-varNames}
137136

138137
To look at the names of variables in a dataframe, use the following syntax:
139138

140139
```{python}
141140
list(mydata.columns)
142141
```
143142

144-
# Logical Operators {#logicalOperators}
143+
# Logical Operators {#sec-logicalOperators}
145144

146145
Logical operators evaluate a condition for each value and yield values of `True` and `False`, corresponding to whether the evaluation for a given value met the condition.
147146

@@ -220,7 +219,7 @@ mydata['prediction'].notnull() & (mydata['parch'] >= 1)
220219
mydata['prediction'].isnull() | (mydata['parch'] >= 1)
221220
```
222221

223-
# Subset {#subset}
222+
# Subset {#sec-subset}
224223

225224
To subset a dataframe, you can use the `loc` and `iloc` accessors, or directly access the columns by their names.
226225

@@ -294,7 +293,7 @@ mydata.iloc[subsetRows][subsetVars]
294293
mydata.loc[mydata['survived'] == 1, subsetVars]
295294
```
296295

297-
# View Data {#viewData}
296+
# View Data {#sec-viewData}
298297

299298
## All Data
300299

@@ -319,7 +318,7 @@ mydata.head()
319318
mydata['age'].head()
320319
```
321320

322-
# Data Characteristics {#dataCharacteristics}
321+
# Data Characteristics {#sec-dataCharacteristics}
323322

324323
## Data Structure
325324

@@ -353,7 +352,7 @@ print(mydata['age'].isnull().sum())
353352
print(mydata['age'].notnull().sum())
354353
```
355354

356-
# Create New Variables {#createNewVars}
355+
# Create New Variables {#sec-createNewVars}
357356

358357
To create a new variable, you can directly assign a value to a new column in the dataframe.
359358

@@ -367,7 +366,7 @@ Here is an example of creating a new variable:
367366
mydata['ID'] = range(1, len(mydata) + 1)
368367
```
369368

370-
# Create a Dataframe {#createDF}
369+
# Create a Dataframe {#sec-createDF}
371370

372371
Here is an example of creating a dataframe:
373372

@@ -380,7 +379,7 @@ mydata2 = pd.DataFrame({ # uses pandas module
380379
mydata2
381380
```
382381

383-
# Recode Variables {#recodeVars}
382+
# Recode Variables {#sec-recodeVars}
384383

385384
Here is an example of recoding a variable:
386385

@@ -404,7 +403,7 @@ for col in columns_to_recode:
404403
mydata[col] = mydata[col].map(lambda x: 1 if x in [0, 1] else 2)
405404
```
406405

407-
# Rename Variables {#renameVars}
406+
# Rename Variables {#sec-renameVars}
408407

409408
```{python}
410409
mydata = mydata.rename(columns = {
@@ -426,7 +425,7 @@ rename_dict = dict(zip(varNamesFrom, varNamesTo))
426425
mydata = mydata.rename(columns = rename_dict)
427426
```
428427

429-
# Convert the Types of Variables {#convertVarTypes}
428+
# Convert the Types of Variables {#sec-convertVarTypes}
430429

431430
One variable:
432431

@@ -442,13 +441,14 @@ Multiple variables:
442441
```{python}
443442
mydata[['age', 'parch', 'prediction']] = mydata[['age', 'parch', 'prediction']].astype(float)
444443
444+
mydata[mydata.loc[:, 'age':'parch'].columns] = mydata.loc[:, 'age':'parch'].astype(float)
445445
446446
# Convert all categorical columns to string
447447
for col in mydata.select_dtypes('category').columns:
448448
mydata[col] = mydata[col].astype(str)
449449
```
450450

451-
# Merging/Joins {#merging}
451+
# Merging/Joins {#sec-merging}
452452

453453
## Overview
454454

@@ -489,7 +489,7 @@ print(mydata2)
489489
print(mydata2.shape)
490490
```
491491

492-
## Types of Joins {#mergeTypes}
492+
## Types of Joins {#sec-mergeTypes}
493493

494494
### Visual Overview of Join Types
495495

@@ -505,7 +505,7 @@ For instance, a left outer join keeps the shared rows and the rows that are uniq
505505

506506
Image source: [Predictive Hacks](https://predictivehacks.com/?all-tips=anti-joins-with-pandas) (archived at: <https://perma.cc/WV7U-BS68>)
507507

508-
### Full Outer Join {#fullJoin}
508+
### Full Outer Join {#sec-fullJoin}
509509

510510
A full outer join includes all rows in $x$ **or** $y$.
511511
It returns columns from $x$ and $y$.
@@ -518,7 +518,7 @@ print(fullJoinData)
518518
print(fullJoinData.shape)
519519
```
520520

521-
### Left Outer Join {#leftJoin}
521+
### Left Outer Join {#sec-leftJoin}
522522

523523
A left outer join includes all rows in $x$.
524524
It returns columns from $x$ and $y$.
@@ -531,7 +531,7 @@ print(leftJoinData)
531531
print(leftJoinData.shape)
532532
```
533533

534-
### Right Outer Join {#rightJoin}
534+
### Right Outer Join {#sec-rightJoin}
535535

536536
A right outer join includes all rows in $y$.
537537
It returns columns from $x$ and $y$.
@@ -544,7 +544,7 @@ print(rightJoinData)
544544
print(rightJoinData.shape)
545545
```
546546

547-
### Inner Join {#innerJoin}
547+
### Inner Join {#sec-innerJoin}
548548

549549
An inner join includes all rows that are in **both** $x$ **and** $y$.
550550
An inner join will return one row of $x$ for each matching row of $y$, and can duplicate values of records on either side (left or right) if $x$ and $y$ have more than one matching record.
@@ -558,7 +558,7 @@ print(innerJoinData)
558558
print(innerJoinData.shape)
559559
```
560560

561-
### Cross Join {#crossJoin}
561+
### Cross Join {#sec-crossJoin}
562562

563563
A cross join combines each row in $x$ with each row in $y$.
564564

@@ -572,7 +572,7 @@ print(crossJoinData)
572572
print(crossJoinData.shape)
573573
```
574574

575-
# Long to Wide {#longToWide}
575+
# Long to Wide {#sec-longToWide}
576576

577577
```{python}
578578
import seaborn as sns
@@ -597,7 +597,7 @@ iris_wide = iris_long.pivot_table(
597597
print(iris_wide)
598598
```
599599

600-
# Wide to Long {#wideToLong}
600+
# Wide to Long {#sec-wideToLong}
601601

602602
Original data:
603603

@@ -621,7 +621,7 @@ iris_long = iris.melt(
621621
print(iris_long)
622622
```
623623

624-
# Average Ratings Across Coders {#avgAcrossCoders}
624+
# Average Ratings Across Coders {#sec-avgAcrossCoders}
625625

626626
Create data with multiple coders:
627627

django.qmd

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ format:
2020

2121
`Django` uses a Model-View-Controller architecture.
2222

23-
### URL Patterns {#urlPatterns}
23+
### URL Patterns {#sec-urlPatterns}
2424

25-
The URL patterns determine which [view](#views) to pass the request to for handling.
25+
The URL patterns determine which [view](#sec-views) to pass the request to for handling.
2626
URL patterns are defined in `urls.py`.
2727

28-
### Views {#views}
28+
### Views {#sec-views}
2929

3030
Views provide the logic or control flow portion of the project.
3131
A view is a `Python` callable, such as a function that takes an `HTTP` request as an argument and returns an `HTTP` response for the web server to return.
32-
Each view we define can leverage [models](#models) and [templates](#templates).
32+
Each view we define can leverage [models](#sec-models) and [templates](#sec-templates).
3333
Views are defined in `views.py`.
3434

35-
### Models {#models}
35+
### Models {#sec-models}
3636

37-
To perform queries against the database, each [view](#views) can leverage `Django` models as needed.
37+
To perform queries against the database, each [view](#sec-views) can leverage `Django` models as needed.
3838
A `Django` model is a class with attributes.
3939
These model classes provide built-in methods for making queries on the associated database tables.
4040
Each model is a database table (i.e., spreadsheet).
@@ -43,9 +43,9 @@ Each database record is a row in the spreadsheet
4343
Models create the data layer of a `Django` app, by defining the schema or underlying structure of a database table.
4444
Models are defined in `models.py`.
4545

46-
#### Defining Fields {#fields}
46+
#### Defining Fields {#sec-fields}
4747

48-
Fields are columns/variables in the database table that are defined by [models](#models).
48+
Fields are columns/variables in the database table that are defined by [models](#sec-models).
4949
Field types and field options are provided in the `Django` documentation here: https://docs.djangoproject.com/en/5.0/ref/models/fields/
5050

5151
Examples of field types include:
@@ -72,7 +72,7 @@ Common field attributes:
7272
- `null`: True or False; determines whether a field can be stored as a null (i.e., there is no data for that field in a given record)
7373
- `choices`: limits the values that can be stored in that field to a set of choices that are provided
7474

75-
#### Migrations {#migrations}
75+
#### Migrations {#sec-migrations}
7676

7777
Migrations create the necessary scripts to change the database structure through time as we update our code to change our models.
7878

@@ -108,7 +108,7 @@ When a migration has been created, but not yet run, we call this an "unapplied m
108108
This is a common source of errors during development, especially when collaborating with other developers.
109109
With this in mind, be sure that when working on a team, to coordinate carefully who is changing which model, and to look for new migration files when pulling in code changes.
110110

111-
### Templates {#templates}
111+
### Templates {#sec-templates}
112112

113113
Each view we define can also leverage templates, which help with the presentation layer of what the `HTML` response will look like.
114114
Each template is a separate file that consists of `HTML` along with some extra template syntax for variables, loops, and other control flow.

0 commit comments

Comments
 (0)