<jupyter_start><jupyter_text>Pandas [![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/davemlz/spyndex/blob/main/docs/tutorials/pandas.ipynb)![level3](https://raw.githubusercontent.com/davemlz/spyndex/main/docs/_static/level3.png) After passing levels 1 and 2, you are ready to start this: Level 3 - `spyndex + pandas`!Remember to install `spyndex`!<jupyter_code>!pip install -U spyndex<jupyter_output><empty_output><jupyter_text>Now, let's start!First, import `spyndex` and `pandas`:<jupyter_code>import spyndex
import pandas as pd<jupyter_output><empty_output><jupyter_text>`pandas.Series`We have all worked with `pandas`. Well, `spyndex` also works with `pandas` so you can continue using it! :)Let's use a `pandas.DataFrame` that is stored in the `spyndex` datasets: `spectral`:<jupyter_code>df = spyndex.datasets.open("spectral")<jupyter_output><empty_output><jupyter_text>Each column of this dataset is the Surface Reflectance from Landsat 8 for 3 different classes. The samples were taken over Oporto:<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Here you can see the classes stored in the `class` column:<jupyter_code>df["class"].unique()<jupyter_output><empty_output><jupyter_text>Each column of the data frame is a `pandas.Series` data type:<jupyter_code>type(df["SR_B2"])<jupyter_output><empty_output><jupyter_text>Well, we can use that to compute Spectral Indices with `spyndex`!Since we have `vegetation`, `water` and `urban` classes, let's compute 3 different indices, each one highlighting an specific class: `NDVI`, `NDWI` and `NDBI`:<jupyter_code>spyndex.indices.NDVI
spyndex.indices.NDWI
spyndex.indices.NDBI<jupyter_output><empty_output><jupyter_text>What bands do we need?<jupyter_code>spyndex.indices.NDVI.bands
spyndex.indices.NDWI.bands
spyndex.indices.NDBI.bands<jupyter_output><empty_output><jupyter_text>Green, Red, NIR and SWIR1 bands... easy!<jupyter_code>parameters = {
    "G": df["SR_B3"],
    "R": df["SR_B4"],
    "N": df["SR_B5"],
    "S1": df["SR_B6"],
}<jupyter_output><empty_output><jupyter_text>With our `dict` of parameters ready we can compute the indices!<jupyter_code>idx = spyndex.computeIndex(["NDVI","NDWI","NDBI"],parameters)<jupyter_output><empty_output><jupyter_text>And, what's the data type of the result?<jupyter_code>print(f"idx type: {type(idx)}")<jupyter_output>idx type: <class 'pandas.core.frame.DataFrame'><jupyter_text>That's right! A `pandas.DataFrame`! Why? Because each computed spectral index is now a column (`pandas.Series`) of a new dataframe:<jupyter_code>idx.head()<jupyter_output><empty_output><jupyter_text>If you want them diectly on the original dataframe as new columns, you just have to play with the code a little bit!<jupyter_code>indicesToCompute = ["NDVI","NDWI","NDBI"]
df[indicesToCompute] = spyndex.computeIndex(indicesToCompute,parameters)<jupyter_output><empty_output><jupyter_text>Now, if you check you original dataframe, you should have the new indices there!<jupyter_code>df.head()<jupyter_output><empty_output><jupyter_text>Beautiful! Right? Now, just for the sake of life, let's make some visualizations!<jupyter_code>import seaborn as sns
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Define some colors for each one of the classes:<jupyter_code>colors = ["#E33F62","#3FDDE3","#4CBA4B"]<jupyter_output><empty_output><jupyter_text>Now, let's create a gorgeous pair grid!<jupyter_code>plt.figure(figsize = (15,15))
g = sns.PairGrid(df[['NDVI', 'NDWI', 'NDBI','class']],hue = "class",palette = sns.color_palette(colors))
g.map_lower(sns.scatterplot)
g.map_upper(sns.kdeplot,fill = True,alpha = .5)
g.map_diag(sns.kdeplot,fill = True)
g.add_legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1345137420718816, 'fraction_numerical': 0.007399577167019027, 'mean_word_length': 8.322660098522167, 'pattern_counts': {'":': 4, '<': 69, '<?xml version=': 0, '>': 69, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '630261', 'license': 'MIT', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1407, 'n_words': 289, 'path': 'docs/tutorials/pandas.ipynb', 'repo_name': 'davemlz/spyndex', 'size': '3784'}