the purpose of this tutorial is to help you get started with Python.
- large datasets
- spatial analysis
- slow r code
- large rasters and satellite imagery
- Google Earth Engine
- parallel processing
01_python_to_r/python_to_r.Rmd- R Markdown file with Python code to translate (no answers)01_python_to_r/python_to_r_with_answers.Rmd- R Markdown file with answers02_python_practice/python_practice.ipynb- Jupyter notebook for practicing Python basics02_python_practice/python_practice_with_answers.ipynb- Jupyter notebook with answers to the practice problems03_python_spatial/spatial_data.ipynb- Jupyter notebook for practicing spatial data analysis
see 00_setup/02_vscode_setup.md for instructions.
see 00_setup/01_python_setup.md for instructions.
https://pre-commit.com/ ensures that you do not push a jupyter notebook to github with output in it. this is important because you may mistakenly push something that should not be on github, or that is too big to be on github. the pre-commit hook will check for output in your jupyter notebooks. if there is output, it will fail, then it will clear the output, then you can add, commit, and push again.
to set this up:
- pip install pre-commit
- pre-commit install
- make sure the .pre-commit-config.yaml file is in the root of your repository. you can copy and paste this exact file into each of your repositories.
practice translating Python code to R using the python_to_r.Rmd file.
instructions:
- open
01_python_to_r/python_to_r.Rmdin VSCode - read through the Python code
- write the equivalent R code in the empty chunk
- check your work against
01_python_to_r/python_to_r_with_answers.Rmd
practice python fundamentals with the jupyter notebook.
instructions:
- make sure your environment is activated:
conda activate practice_env - open
02_python_practice/python_practice.ipynbin VSCode - work through the exercises, running each cell with Shift+Enter
- complete the practice problems at the end
practice spatial data analysis with the jupyter notebook.
instructions:
- make sure your environment is activated:
conda activate practice_env - open
03_python_spatial/spatial_data.ipynbin VSCode - work through the exercises, running each cell with Shift+Enter
- complete the practice problems at the end
| task | python (pandas) | r (tidyverse) |
|---|---|---|
| read CSV | pd.read_csv("file.csv") |
read_csv("file.csv") |
| view head | df.head() |
head(df) |
| column mean | df['col'].mean() |
mean(df$col) |
| filter rows | df[df['col'] > 5] |
filter(df, col > 5) |
| select columns | df[['a', 'b']] |
select(df, a, b) |
| rename column | df.rename(columns={'old': 'new'}) |
rename(df, new = old) |
| new column | df['new'] = df['a'] + df['b'] |
mutate(df, new = a + b) |
- indentation matters in Python! use 4 spaces (not tabs) for code blocks
- 0-indexed: python counts from 0, R counts from 1
- assignment: Python uses
=, R uses<-(though=also works) - use
print(): unlike R, Python won't automatically display results in scripts
"module not found" error:
pip install module_nameconda environment issues:
conda deactivate
conda activate practice_envjupyter can't find kernel:
pip install ipykernel
python -m ipykernel install --user --name=practice_env